[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Apache mod_rewrite questions

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

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.
  • 3
1 Solution
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.


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 .
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
sara_bellumAuthor Commented:
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.
sara_bellumAuthor Commented:
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]
sara_bellumAuthor Commented:
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.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now