Question

I am trying to use mod_rewrite to change the appearance of my URL's. I am really lost in this and could use some help. First of all, is it possible to use mod_rewrite to alter the "front-end" of a URL and keep it the same internally? If so, how would I do the following:

I have https://domain.com/live/page.php?id=x with both "page" and "x" being variable, and would like the URL's to be shown as https://domain.com/page if possible, or if I need the id=x in there then it could be https://domain.com/page/x/ but I don't know if I need to have that id somewhere in the URL.

Please help me as I know this is a common thing but I just can't figure it out!

Thanks!

EDIT: With the help of anubhava I got this working halfway. This time I put the code into my .htaccess for the /live/ directory not the root one. Now the pages change URL correctly, but I still need the server to load from the original so the new URL's are giving me a 404. Here is the code:

RewriteEngine on

RewriteOptions inherit

RewriteCond %{THE_REQUEST} \s/+live/([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /live/$1.php?$2=$3 [L,QSA,NC]

EDIT: Here are my current .htaccess files

root:

RewriteEngine on

RewriteOptions inherit

RewriteCond %{HTTP_HOST} ^bidwaffle\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bidwaffle\.com$

RewriteRule ^/?$ "https\:\/\/bidwaffle\.com\/live" [R=301,L]

and in root\live\:

# secure htaccess file
<Files .htaccess>
 order allow,deny
 deny from all
</Files>

# disable directory browsing
Options All -Indexes

# disable access to logs/template files
<Files ~ "\.(log|tpl)$">
 order allow,deny
 deny from all
</files>

RewriteCond %{THE_REQUEST} \s/+live/([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /live/$1.php?$2=$3 [L,QSA,NC]

The URL changes properly but doesn't load properly (silent loading of the real URL). Right now I see the issue of the second part where it may not even see this request because the /live/ is no longer in the "pretty URL" so the /live/.htaccess might not be called. I tried putting that part into the root/.htaccess and it did the same weird thing with appending the referring page instead of %3

Was it helpful?

Solution

Yes sure you can have rules like this in your DOCUMENT_ROOT/live/.htaccess file:

RewriteEngine On
RewriteBase /live/

RewriteCond %{THE_REQUEST} /([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L]

root .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?bidwaffle\.com$
RewriteRule ^/?$ https://bidwaffle.com/live/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /live/$1.php?$2=$3 [L,QSA,NC]

OTHER TIPS

I think you've got it backwards. The incoming URI from visitors (and links on your pages) is the SEO (pretty) form: /page/x. It is the job of .htaccess to convert the pretty form into the real "dynamic" form: /live/page.php?id=x (where both page and x could be extracted from the SEO URI). .htaccess does not modify outgoing URIs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top