I am trying to hide .php extension and also rewriting the url string with trailing slashes. File extension hiding is working fine but url string is not.
From:
http://www.example.com/abc.php?id1=1&id2=2
To:
http://www.example.com/abc/id1/1/id2/2
Following is my .htaccess
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L]
First, check %{THE_REQUEST} variable. Redirect if it satisfies your match conditions.
RewriteCond %{THE_REQUEST} ^GET\ /abc\.php\?(id1)=(\d+)&(id2)=(\d+) [NC]
RewriteRule ^ /abc/%1/%2/%3/%4? [R=301,L]
Next, internally rewrite the friendly-url.
RewriteRule ^abc/(id1)/(\d+)/(id2)/(\d+)$ /abc.php?$1=$2&$3=$4 [NC,L]
Related
I have a URL that has already been rewritten to hide my .php extensions. I am now having issues when trying to further rewrite the URL.
My current URLs (note the forward slash before the query string):
https://example.com/card2/?search=Jimmmy
Here is what I have currently tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2.php?search=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /card2.php\?search=([^\&\ ]+)
RewriteRule ^/?card2\.php$ /card2/%1? [L,R=301,NE]
This works if I try this URL (I have to manually remove the forward slash before the query string and type in .php):
https://example.com/card2.php?search=Jimmy
So the only way I can get it to work at the moment is by typing in .php in the URL and removing the forward slash after that.
I have tried the following, my logic was to just remove the .php extensions that were in the rewrite and add the forward slash too but it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2/?search=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /card2\/\?search=([^\&\ ]+)
RewriteRule ^/?card2$ /card2/%1? [L,R=301,NE]
EDIT, thanks to anubhava for the answer, here is my htaccess file now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
ErrorDocument 404 /not-found.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
<filesMatch "\.( jpg|jpeg|gif|png|ico|js)$">
Header set Cache-Control "max-age=2419200, public, must-revalidate"
</filesMatch>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 70.24.57.210
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2/?search=$1 [L]
RewriteCond %{THE_REQUEST} \s/card2(?:\.php)?/?\?search=([^\&\s]+) [NC]
RewriteRule ^ /card2/%1? [L,R=301,NE]
Have your redirect rule like this with optional matches:
RewriteCond %{THE_REQUEST} \s/card2(?:\.php)?/?\?search=([^\&\s]+) [NC]
RewriteRule ^ /card2/%1? [L,R=301,NE]
Also keep this this rule at top of your .htaccess and test from a new browser.
I did a php blog and I would like to have friendly SEO urls for my posts, and also remove .php extension from my urls. So I edited my htaccess file like this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [N]
#second condition and rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So I got my friendly SEO post urls but the second condition doesn't work. But it works when I use it as my first condition. What I did wrong ? Thanks a lot for your help
Keep first rule as .php handler rule but that should check for presence of .php file as below:
RewriteEngine On
# if not a directory and file.php exists then rewrite file to file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# rewrite every non-file or non-directory to viewpost.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ viewpost.php?id=$1 [L,QSA]
I need to change old dynamic urls to new, so I have created .htaccess file, but something wrong and redirect to ERR_TOO_MANY_REDIRECTS problem.
Below my .htaccess file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/(.+)/$ /cat.php?name=$1 [QSA,L]
RewriteCond %{QUERY_STRING} ^name=(.*)$ [NC]
RewriteRule ^cat\.php$ /movie/%1/? [R=301,L]
You need to check the original URI, and redirect to the pretty URL from that:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /cat\.php\?name=([^\s&]+) [NC]
RewriteRule ^cat\.php$ /movie/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/(.+)/$ /cat.php?name=$1 [QSA,L]
I would like to redirect link http://example.com/asdasdas/edit to http://example.com/asdasdas/index.php/edit using htaccess. I did this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} edit
RewriteRule ^ %{REQUEST_URI}/index.php [L,R=301]
but it's a loop and doesn't works well. Any ideas?
You can use this rule in /asdasdas/.htaccess:
RewriteEngine On
RewriteBase /asdasdas/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php/$ [L]
This will support URL of http://example.com/asdasdas/edit
currently I am using this htaccess access,but the site still loads for invalid url, it doesn't goto 404.
ex: http://www.couponcoder.in/babyoye.com -->works and main content
http://www.couponcoder.in/babyo ----> invalid url, it displays homepage
and site loads for both slash and without slash, I just want it to redirect non-slash
www.couponcoder.in/babyoye.com/
www.couponcoder.in/babyoye.com
can someone help out with this?
Options +Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?couponcoder\.in$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1couponcoder.in%{REQUEST_URI} [R=301,L]
RewriteRule ^admin$ Admin/index.php?qstr=$1 [L]
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?qstr=$1 [L]
I can't read if you want to have the non-slash version or if you want to redirect the non-slash version to the slashed one. I'm assuming you want the former.
...
#remove slash, and redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?qstr=$1 [L]