Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • 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
</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
sara_bellum
Asked:
sara_bellum
  • 3
1 Solution
 
InsoftserviceCommented:
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
 
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.
0
 
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]
0
 
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.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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