Forum Moderators: phranque

Message Too Old, No Replies

htaccess problem with mod rewrite

         

qhrome

10:59 pm on Apr 26, 2011 (gmt 0)

10+ Year Member



Hello,

I am trying to create an .htaccess file that will take a URL like:
http://www.mydomain.com/referringSite

and turn it into this:
http://www.mydomain.com/index.php?main_page=login&referrer=referringSite


my .htaccess file looks like this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule http://www.mydomain.com/(.+)/ http://www.mydomain.com/index.php?main_page=login&referrer=$1 [NC]


I have also tried it like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/(.+)/ http://www.mydomain.com/index.php?main_page=login&referrer=$1 [NC]


But neither of these solutions seem to work. Could someone please tell me what I am doing wrong? Or just give an example of something that will work.

g1smd

11:08 pm on Apr 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The first rule can never work because RewriteRule cannot test for domain name nor query string.

The second rule works only when requested URL contains
/<something>/<anything-or-nothing>


However, I am not sure why you want to redirect any SEO-friendly URL to a long and complicated parameterised unfriendly URL.

Both rules produce a 302 redirect.

The [L] flag is missing from both rules.


Let's get this clear.

1. What is the URL the user requests? Use example.com here to stop forum auto-linking.

2. After the request, do you want the user to see the browser URL bar change to a different URL (i.e. a redirect) or do you want an internal rewrite (where the URL bar does not change)?

3. What is the server path where the content really resides? State this as an internal server filepath, and do NOT include any domain name.

qhrome

12:22 am on Apr 27, 2011 (gmt 0)

10+ Year Member



Thanks for the info. To answer your questions:

1. the user would request example.com/promotionName

2. I do not want the user to see the browser URL change, I just want my PHP file to be able to read promotionName from the URL. I could just explode the URL to retrieve the variable from example.com/promotionName , but the problem is that there is no real directory called "promotionName", so it gets a 'Not Found' error.

3. The server path where the content really resides is:
example.com/index.php?main_page=login (it is a modified Zen Cart installation, and the main_page variable is required to get to the page I want)
I was attempting an internal redirect to
example.com/index.php?main_page=login&referrer=promotionName
In order to pass that variable.

If promotionName is present, the login page is customized based on that variable.

g1smd

12:34 am on Apr 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



3. If you include the domain name in a RewriteRule you ALWAYS get an EXTERNAL redirect. The default is a 302 redirect.

That's why my question said "State this as an internal server filepath, and do NOT include any domain name".

So that's the problem in a nutshell.

The solution?

You need an internal rewrite:

RewriteRule ^([^/.]+)$ /index.php?main_page=login&referrer=$1 [L]


Notice that there is no domain name in this code. The code maps an external request to an internal file.

qhrome

12:49 am on Apr 27, 2011 (gmt 0)

10+ Year Member



That code works perfectly! Thank you very much for writing the working example, and for the explanation of why mine wasn't working.

g1smd

8:12 am on Apr 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One you have these six concepts nailed you can do anything.

1. URLs are a reference system used on the web and include a domain name. A fully-qualified URL will contain at least the protocol and domain.

2. Server filepaths are a reference system used inside the server. Inside the server there is NO concept of a domain name. They are just folders and files.

3. An external redirect is a response sent back to the browser suggesting that it makes a new HTTP request for a different URL.

4. The action of a server is to map an external URL request to a folder and file inside the server. In normal operation, the filename fetched from the server will have the same filename as the one seen in the URL.

5. An internal rewrite accepts an incoming URL request and then silently fetches content from inside the server from a different location to that suggested by the path part of the URL request.

6. Mod_rewrite cannot change the links on your pages. The links on your pages should contain the URLs that you want your users to see and use. URLs are defined in links.

g1smd

9:43 am on Apr 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



typo: one --> once