Solved

Apache mod_rewrite questions

Posted on 2014-03-21
4
238 Views
Last Modified: 2014-04-04
I have a site on a shared hosting server that is using .htaccess to redirect mydomain.com to www.mydomain.com and to rewrite mydomain.com to mydomain.com/chs (the chs  subdirectory is my doc root).

I believe that having a cgi-bin directory under my apache doc root is a bad idea, but my hosting company appears unconcerned and I can't see that I have a choice. Let me know what the risks are, thanks.

I tried using apache mod_rewrite to rewrite the url to my scripts directory:
http://www.mydomain.com/chs/my_scripts/myscript.cgi (current url)
http://www.mydomain.com/chs/abc/myscript.cgi (desired url)

with this apache configuration:

DocumentRoot /home/user/path/to/chs
Alias /abc /home/user/path/to/chs/my_scripts

<Directory /home/user/path/to/chs/my_scripts>
    RewriteEngine On
    RewriteRule ^/abc/(.*)$ /my_scripts/$1
    AllowOverride All
</Directory>

Open in new window


but the urls to my scripts remain unchanged and no errors appear in the logs (with level  warning). I'm trying to accomplish something covered under http://www.experts-exchange.com/Web_Development/Components/Q_27685973.html and can't see what I'm missing. Help.
0
Comment
Question by:sara_bellum
  • 3
4 Comments
 
LVL 15

Accepted Solution

by:
Insoftservice earned 500 total points
ID: 39944910
Hi,
Little confused with your question.
"I have a site on a shared hosting server that is using .htaccess "

and you have changed apache config.
Did you changed the conf file from your hosting provider?

    RewriteRule ^/abc/(.*)$ /my_scripts/$1
Allows the browser to accept the url which has "abc" within it instead of "my_scripts".
Try to create url replace with my_scripts and try whether you are getting any error.
As rewrite would not replace your url but it allows you to use your desired url considering your rewrite rule is right.

Conclusion

If you are able to use you new url it implies that your rewrite rule is perfect.
1>Try to change url link wherever it has "myscripts" within it .
                    Or
if you are getting "mysripts" within the page then do 301 redirect to "abc" so that your users won't get two different url and even google does not have any issue for its SEO
0
 

Author Comment

by:sara_bellum
ID: 39946404
Great advice, thank you! It worked perfectly on localhost, where I can edit the site file directly in apache. I hadn't thought of testing the url manually and was following links from previous pages. Once I changed those links as you recommended, all was well.

Unfortunately, mod_rewrite implementation in the htaccess files on the remote server fails and I haven't figured out how to make it work.  Any tips are appreciated.
0
 

Author Comment

by:sara_bellum
ID: 39946664
I'm still confused about how this works: in my localhost apache site config directory instructions that correctly implement the url rewrite, the syntax is:
RewriteRule: actual-path requested-path

But in your htaccess guidance this order is reversed:
RewriteRule: requested-path actual-path
Please confirm the path order that htaccess uses for rewrites, thanks.

To recap, htaccess is now enabled on my localhost and my htaccess file is under doc root (my hosting provider checked for htaccess errors under doc root only):
RewriteBase: /abc (desired path)
RewriteRule /abc  /my_scripts

Apache looks for the file under the desired url:
/path/to/docroot/abc/text.cgi and cannot find it - the actual path is
/path/to/docroot/my_scripts/text.cgi - if I change the url back to the actual path it works on localhost but on the remote server I get an apache core error with rewrite enabled.

I tried reversing this order in localhost htaccess:
RewriteBase /my_scripts (actual path)
RewriteRule /my_scripts /abc
but the error is the same. Apache simply compares the requested url to the actual path and no rewrite occurs.

The syntax remains unchanged from my earlier post:
RewriteBase /abc/
RewriteRule ^/abc/(.*)$ /my_scripts/$1 [L]
0
 

Author Comment

by:sara_bellum
ID: 39976928
This turned out to be more difficult than anticipated. Initially, I got this rule to work in my apache doc root .htaccess file:
RewriteRule ^desired-path/(.+)$ /actual-path/$1

but then I decided that I needed to remove the joomla subdirectory from my urls (joomla was installed in a subdirectory of my apache doc root). This required that I  move the joomla htaccess file under my apache doc root and edit this file under the 'custom redirects' section.  It also required that the configuration.php file $live_site variable reflect the desired url.

I also installed an SSL certificate for the domain, again editing the configuration.php  $live_site variable to reflect https connections.

The url now appears secure and omits the joomla subdirectory as intended, but the rewrite rule for my script directory fails. Also, requests for https://mydomain.com are not redirected to https://www.mydomain.com, although www redirects worked when I was using http connections, due to the way secondary domains are configured for multiple-domain SSL certificates with my hosting service.

So it's work in progress. But it's time to close this.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now