I recently tried to hide parameters from my url, using htaccess, ended up with a 500 Server Error.
Here is my .htaccess code:
<files .htaccess>
order allow,deny
deny from all
</files>
Options -Indexes +FollowSymLinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteBase /
RewriteRule ^([^/]+\.php)/.* $1 [R=301,L]
RewriteRule ^buyit\.php$ /buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 [L]
All I wanted to do is my page buyit.php?qsta=1&gid=11-64-36&qid=4&kaq=0&req=14&dlk=7 to show always as buyit.php keeping my variables intact and usuable for my php scripts. So I added this last line in htaccess but unfortunately it crashes.
Any ideas why, please?
Your code is looping. This is because you can't match the query string in a rewrite rule, thus a URL like:
/buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7
actually matches:
^buyit\.php$
So you need to add a condition to check that there isn't a query string. Change your last rule to this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^buyit\.php$ /buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 [L]
Unfortunately, if you are passing values using GET method, you cant simply get rid off the parameters. However, you can remove the parameters name and make the url unique and NOT EASILY UNDERSTANDABLE by some one who doesn't know about the parameter names.
Try this htaccess code.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /buyit.php?qsta=$1&gid=$2&qid=$3&kaq=$4&req=$5&dlk=$6 [L]
It converts your URL from this :
http://yourdomain.com/buyit.php?qsta=1&gid=11-64-36&qid=4&kaq=0&req=14&dlk=7
to this
http://ydomain.com/1/11-64-36/4/0/14/7.html
which is also good for your SEO.
Related
Hey so I'm trying to get my htaccess file to do 2 things:
1) if user enters http://dstealth.com/i or http://dstealth.com/i/some/other/address I want htaccess file to use http://dstealth.com/z_index/ or http://dstealth.com/z_index/some/other/address
2) I also want the htaccess file to redirect the user if they use the old address. eg if user types http://dstealth.com/z_index or http://dstealth.com/z_index/some/other/address I want the URL to appear as http://dstealth.com/i/some/other/address
Basically at all times replace z_index with i even though the directory i does not exist and z_index is still the directory being used.
I've tried a bunch of stuff and it seems not to be working as it keep redirecting me to my 404 page.
My .htaccess file:
ServerSignature Off
ErrorDocument 403 /error_pages/403.php
ErrorDocument 404 /error_pages/404.php
ErrorDocument 500 /error_pages/500.php
IndexIgnore *
RewriteEngine On # Turn on the rewriting engine - only needed once
Options -MultiViews
# Prevent directory listings
Options All -Indexes
# Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# Rewrite www.dstealth.com as dstealth.com
RewriteCond %{HTTP_HOST} ^www\.dstealth\.com [NC]
RewriteRule ^(.*)$ http://dstealth.com/$1 [R=301,NC]
# Rewrite /z_index/ as /i/
RewriteRule ^z_index/([^/.]+)$ /i/$1 [NC]
RewriteRule ^i/([^/.]+)$ /z_index/$1 [NC,L]
results:
http://dstealth.com/z_index/ still appears exactly the same and doesnt change to http://dstealth.com/i/
http://dstealth.com/i/ takes me to my 404 page :(
I believe I got what I want with the following code. Please let me know if it has any errors or inefficiencies:
RewriteCond %{THE_REQUEST} \s/z_index [NC]
RewriteRule ^z_index/([^.]+)\.php$ /i/$1\.php [R=302,L]
RewriteCond %{THE_REQUEST} \s/z_index [NC]
RewriteRule ^z_index/([^.]+)$ /i/$1 [R=302,L]
RewriteCond %{THE_REQUEST} \s/z_index [NC]
RewriteRule ^z_index/?$ /i/ [R=302,L]
RewriteRule ^i/([^.]+)\.php$ /z_index/$1\.php [NC,L]
RewriteRule ^i/$ /z_index/?&%{QUERY_STRING} [NC,L]
i am doing a URL-Shortener where i am trying to do it URL friendly.. Currently my adress is typed like this "example.com/index.php?name=sitename" i want it do be like "example.com/sitename".. I guess i could use some cool rewrite rule but i cant get it to work..
I did something like this but it wont work
Options All -Indexes
DirectoryIndex index.php index.html index.htm
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?name=$1 [L]
but i dont know :s?
Here is the code you need to place in DOCUMENT_ROOT/.htaccess
Options All +FollowSymLinks -MultiViews -Indexes
DirectoryIndex index.php index.html index.htm
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?name=$1 [L,QSA]
Reference: Apache mod_rewrite Introduction
Your regular expression will fail on URLs with more than one / after the domain, because [^/] stops as soon as it sees one of these characters, and then it looks for the last slash with /$, which is not always the case.
Assuming you want to convert these URLs
example.com/HiThere
example.com/Your/Page
to:
example.com/index.php?name=HiThere
example.com/index.php?name=Your/Page
Try this rewrite rule instead:
RewriteRule ^(.*?)/$ /index.php?name=$1 [L]
This will look for all characters with (.*?) UNTIL it reaches the last /. I'm not 100% sure about the [L] flag, though.
the simplest method is to use 404 error to point to index.php and then get the sitename using php
.htaccess file
ErrorDocument 404 /index.php
PHP CODE
<?php
$sitename=substr(strrchr($_SERVER["REQUEST_URI"],"/"),"1");
now simply visit 'example.com/sitename' and sitename will be stored in $sitename.
And if($sitename==""){//Show your HomePage}
Hope it helps!!
I want to remove the query strings from the urls using Htaccess, I used the following code for changing the urls, It did but after redirection to that url, I am getting 404 error.
and there is rewrite statement also if i use that only, then the new url works without 404 error, but the old urls doesnt get automatically redirected to new urls.
Here is the htaccess and url the I am modifying
Options FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/profile\.php$
RewriteCond %{QUERY_STRING} ^user_id=([0-9]*)$
RewriteRule ^(.*)$ http://www.meenmipage.com/user/%1? [R=302,L,NC]
Actual Url was:
http://www.meenmipage.com/profile.php?user_id=2
and modified was:
http://www.meenmipage.com/user/2
If i remove the above code and just use the rewrite statement as like this one:
RewriteRule ^user/([^/]*)$ /profile.php?user_id=$1 [NC,L]
Then the new modified url works and the old one also works
Please tell me what to do?
I think you need to remove $ at the end of this line:
RewriteCond %{REQUEST_URI} ^/profile\.php
because if the request URI has user_id=2 then this condition will not match
Try This !
-----------
Options -MultiViews
Options +FollowSymlinks
RewriteEngine on
rewritecond %{QUERY_STRING} ^user_id=([0-9]*)
rewritecond %{http_host} ^www.meenmipage.com [nc]
RewriteRule ^([0-9]+)$ //www.www.meenmipage.com?user_id=$1 [L,QSA]
i want rewrite it with a .htaccess i have this url:
../index.php?page=details&id=123456
like this:
../detail/123456
but I do not really know how to do this. Could you help me to rewrite this url?
or give me a simple example that I can understand how it works
RewriteRule ^(.*)/([0-9]+)$ index.php?page=$1&id=$2&%{QUERY_STRING}
The first (.*) selects any string, e.g. "details". ([0-9]+) catches a number, in your case "123456"
Finally
&%{QUERY_STRING}
ensures, that additional parameters are added to your backendscript, too.
This should work:
RewriteEngine On
RewriteRule ^detail/([^/]*)$ /index.php?page=details&id=$1 [L]
Here is an example I use for my webpage when it's in a subdirectory.
# "learn/degree/index.php?d=mathematics" shows as "learn/degree/mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
To complete it, remember to write at the beginning of the page this:
RewriteBase /
RewriteEngine On
Here's the 'full' .htaccess I use in my page to give you some idea.
#Redirect any www. to the page without it. Helps to keep user logged.
RewriteBase /
RewriteEngine On
rewriteCond %{HTTP_HOST} ^www.newfutureuniversity.org [NC]
rewriteRule ^(.*)$ http://newfutureuniversity.org/$1 [R=301,L]
Options +Indexes
Options +FollowSymlinks
# Finally working. Rewrite the user so "/student/Username" will internally be "/student/?user=Username"
RewriteRule ^student/([a-zA-Z0-9_-]+)$ student/index.php?user=$1
# Rewrite the disciplines so "learn/degree/1" will internally be "learn/degree/index.php?dis=1"
RewriteRule ^learn/degree/([1-4])$ learn/degree/index.php?dis=$1
# Rewrite the degrees so "learn/degree/mathematics" will internally be "learn/degree/index.php?d=mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
# Rewrite the degrees so "learn/subject/2/5/7" will internally be "learn/subject/index.php?class=2&div=5§ion=7"
RewriteRule ^learn/subject/([0-9])$ learn/subject/index.php?class=$1
RewriteRule ^learn/subject/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2
RewriteRule ^learn/subject/([0-9])/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2§ion=$3
You have to make the links to ../detail/123456, then the script interpretes it internally as ../index.php?page=details&id=123456. If you don't want this, then you are looking for a redirect.
Try this
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
# RewriteBase / add this if necessery, commented intentionally
RewriteRule ^detail/([0-9]+)$ index.php?page=details&id=$2
</IfModule>
Here's my htaccess file at the moment:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /blog/
ErrorDocument 404 /blog/404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ post.php/$1.html
RewriteRule ^index$ index.php
RewriteRule ^archives$ archives.php
RewriteRule ^about$ about.php
RewriteRule ^search$ search.php
RewriteRule ^feed$ feed.php
At the moment if I go to domain.com/blog/*.html, it is redirecting (in the background) to domain.com/blog/post.php/*
I would like to almost copy this but I'm not sure about all this regular expression stuff.
I would like to be able to also go to domain.com/blog/admin/* and for it to redirect (in the background) to domain.com/blog/admin.php?p=*
If you guys could help me out, I would really appreciate it!
Thanks
Something like:
RewriteRule ^/?admin/(.*)$ admin.php?p=$1&%{QUERY_STRING} [L,NC]
Will do the trick.
This is done very quickly and it isn't tested but it gives you the idea. You put this above:
RewriteRule ^(.*)\.html$ post.php/$1.html
Edit: Took out the rewritecond since I just realised it is useless
Edit: Added query string too since you are actually converting the _GET now.
Add multiviews to the first line of your .htaccess:
Options FollowSymLinks MultiViews
Then, restart apache. It should point admin to admin.php.
Note: Don't have multiple files in the same directory with the same name and different extension.
http://httpd.apache.org/docs/2.2/mod/mod_negotiation.html