Apache mod_rewrite questions

Posted on 2014-03-21
Last Modified: 2014-04-04
I have a site on a shared hosting server that is using .htaccess to redirect to and to rewrite to (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: (current url) (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 and can't see what I'm missing. Help.
Question by:sara_bellum
  • 3
LVL 15

Accepted Solution

Insoftservice earned 500 total points
ID: 39944910
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

Author Comment

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.

Author Comment

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]

Author Comment

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 are not redirected to, 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Free online wiki for personal use 6 134
Change ORACLE DB Gregorian / Persian calender... 1 603
Lenovo P500 Dim Display 9 225
WCAG audit tools 1 72
I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

15 Experts available now in Live!

Get 1:1 Help Now