rewrite url after remove extension - php

I need your help.
I have a problem to rewrite a dynamic url after extension removed, I've rewritten this url but not works:
.htaccess like this:
<IfModule mod_rewrite.c>
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#add www 1
RewriteCond %{HTTP_HOST} !^www\.mondomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mondoamin.com/$1 [R=301,L]
#remove .php extension 2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# Removes index.php from ExpressionEngine URLs 3
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]
#rewrite url 4
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /article\.php\?id_ article =([^&\ ]+)([^\ ]*)
RewriteRule ^/article/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php $1 [R=301,L]
</IfModule>
results:
1+2+3 works fine but 4 not works
Thanks in advance

Try reordering the rules and keep 301 rules before internal rewrites:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#add www 1
RewriteCond %{HTTP_HOST} !^www\.mondomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mondoamin.com/$1 [R=301,L]
#rewrite url 4
RewriteCond %{THE_REQUEST} /article\.php\?id_article=([^&\ ]+)([^\ ]*)
RewriteRule ^ /article/%1?%2 [L,R=301]
RewriteRule ^article/([^/]+)/?$ /article.php?id_article=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php $1 [R=301,L]
#remove .php extension 2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
# Removes index.php from ExpressionEngine URLs 3
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]

this is the my last .htaccess code:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
add www
RewriteCond %{HTTP_HOST} !^www.mondomain.fr$ [NC]
RewriteRule ^(.*)$ http://www.mondomaine.fr/$1 [R=301,L]
rewrite dynamic url
RewriteCond %{THE_REQUEST} /article.php\?id_article=([^&\ ]+)([^\ ]*)
RewriteRule ^ /article/%1?%2 [L,R=301]
RewriteRule ^ article/([^/]+)/?$ /article.php?id_article=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)[^.#?\ ]+.php([#?][^\ ])?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+).php $1 [R=301,L]
remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.?)index.php/(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]

Related

find and replace all the ? and =by / htaccess

hey all i am new to htaccess and i need to find and replace all the occurence of ? and = by / from the url by htacess
in my htaccess file the code is
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+page\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page.php?id=$1 [L,QSA]
there are some pages on the site like page2.php?id=34
currently it is showing like page2?id=34 but i want it like page2/id/34 and same for all the other pages
You have everything you need already in your htaccess almost. You direct everything except files and directories that do not exist to page.php
Then in your PHP you rewrite urls like /page.php/id/2 to what you want.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /page.php$1 [L,QSA]
You then just need to parse these url's in PHP inside page.php or index.php as many people use.
<?php
$request = str_replace("/page.php", "", $_SERVER['REQUEST_URI']);
$params = split("/", $request);
?>
Further reading on the idea of pretty urls here: http://forum.codecall.net/topic/74170-clean-urls-with-php/ and http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
You can have:
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+page\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
### rules to convert ?n1=v1&n2=v2 /n1/v1/n2/v2
RewriteCond %{THE_REQUEST} /page2\.php\? [NC]
RewriteCond %{QUERY_STRING} ^([^&]+)&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]
RewriteCond %{ENV:REDIRECT_QS} =1
RewriteCond %{QUERY_STRING} ^([^=]+)=(.*)$
RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]
RewriteCond %{ENV:REDIRECT_QS} =1
RewriteCond %{QUERY_STRING} ^[^&=]+$
RewriteRule ^(page2)\.php$ /$1/%{QUERY_STRING}? [L,NE,R=302]
# recursion rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule ^(page2)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$ /$1.php$4?$2=$3 [L,QSA]
### end of convert ?n1=v1&n2=v2 /n1/v1/n2/v2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page.php?id=$1 [L,QSA]

htaccess migration from Apache to LiteSpeed

my htaccess rules works fine in Apache but i had to change to LiteSpeed and my page doesnt work properly (eg if i type url like http://domain.com/article/some-article or http://domain.com/article/tags/some-tag). Maybe somebody coped with similar problem? In server logs i get : Maximum number of redirect reached.
My htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond %{REQUEST_URI} .article.php/tags/([^/]+)$ [NC]
RewriteRule ^(.*)$ article.php?tag=%1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} .article.php/([^/]+)$ [NC]
RewriteRule ^(.*)$ article.php?art=%1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} .article.php/(.+)$ [NC]
RewriteRule ^(.*)$ http://domain.com/article/$1 [L,R=301]
I will be grateful for your help
I show you how I coped with the problem - I had to change rules and order - I think there was some loop redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NC]
RewriteCond %{REQUEST_URI} .articles/tags/([^/]+)$ [NC]
RewriteRule ^(.*)$ articles.php?tag=%1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} .articles/([^/]+)$ [NC]
RewriteRule ^(.*)$ articles.php?article=%1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} .articles.php/tags/([^/]+)$ [NC]
RewriteRule ^(.*)$ articles/tags/%1 [L,R=301]
RewriteCond %{REQUEST_URI} .articles.php/([^/]+)$ [NC]
RewriteRule ^(.*)$ articles/%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
#301 redirect error page (instead of 302 - for SEOD reason)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /page-not-found [L,R=301]
</IfModule>

.htaccess rewrite not working 301redirect

I have the following code in my .htaccess to rewrite the username.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1 [L]
</IfModule>
So this is the original url:
www.domain.com/profile.php?username='Mike'
Into this:
www.domain.com/Mike
Now I have another rewrite rule for www 301 redirect.
This also works great except when I'm at wwww.domain.com/Mike and I delete the 'www' and this is what I'm getting in my url:
www.domain.com/profile.php?username=Mike
My question is why im not getting the same url:(www.domain.com/Mike) after deleting 'www'?
Here is my full code in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1 [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
Change order of your rules to keep 301 redirect rules before internal rewrite ones:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1 [L,QSA]

I want to redirect domain.com/index.php to domain.com

How to redirect mydomainname.com/index.php to mydomainname.com
Currently I use following codes also.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mydomianname\.com
RewriteRule (.*) http://mydomianname.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule>
This should be your complete .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mydomianname\.com
RewriteRule (.*) http://mydomianname.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule>
This example only works for http, but should roughly do the trick.
# Redirect requests with index.php to the corresponding request
# without the index.php
RewriteRule ^index.php(.*) http://mydomianname.com$1 [R=301,L]

Friendly url with mod_rewrite

I have an URL like this
http://localhost/rilo/listing.php?id=2
where "rilo" is the root and I'd like to get friendly URL like
http_//localhost/rilo/listing/2.html
And here's the htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /rilo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?listing/([a-zA-Z_]+).html$ listing.php?id=$1 [QSA,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Can you help me? Where is the error?
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /rilo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?listing/([^.]+)\.html$ listing.php?id=$1 [QSA,L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+[^/]+/(listing)\.php\?id=([^&\s]+) [NC]
RewriteRule ^ %1/%2.html? [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Categories