HTACCESS - mod_rewrite - php

I would like to rename url www.mypage.com/index.php or www.mypage.com/index_cz.php (the same version of index - just in another language) to www.mypage.com/en or www.mypage.com/cz
I have this code in .htaccess
RewriteEngine On
RewriteRule ^index\.php$ /en/ [R=301,L]
RewriteRule ^index_cz\.php$ /cz/ [R=301,L]
It redirects me to www.mypage.com/en or www.mypage.com/cz but it gimme back NOT FOUND...
Really thanks for ur help

You can use:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php[?/\s]
RewriteRule ^index\.php$ /en/ [R=301,L]
RewriteCond %{THE_REQUEST} /index_cz\.php[?/\s]
RewriteRule ^index_cz\.php$ /cz/ [R=301,L]
RewriteRule ^/en/?$ index.php [NC,L]
RewriteRule ^cz/?$ index_cz.php [NC,L]

Related

Need to write multiple htaccess rule with multiple parameters

I've some rules already written in htaccess to convert .php extension to HTML and they are working fine but now I need to write rule for different page with same parameter but it if I set same rule for different page its not working its redirecting on same page.
Below is my htaccess file code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain/
# add everything for webservice/...
RewriteRule ^webservice(/.*)?$ - [L,NC]
#redirect localhost/apnaujjain/blog.php?page_id=1&post_id=1&action=blog to localhost/apnaujjain/1/1/blog.html
#RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
#RewriteRule . %1/%2/%3.html? [R=301,L]
#RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ blog.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
#redirect localhost/apnaujjain/page.php?page_id=1&album_id=1&action=contacts to localhost/apnaujjain/1/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
#redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]
#redirect localhost/apnaujjain/contacts.php to localhost/apnaujjain/contacts.html
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
#RewriteRule ^(admin|webservice)($|/) - [L]
RewriteRule !^admin/ /%1.html [NC,R=302,L,NE]
RewriteRule ^(.+?)\.html$ $1.php [L,NC]
I have already written comment on every rule for what its doing. Now currently all rules are working but when I added other rule it stopped working so I just commented that rule.
Rule for page.php is working but when I change page to blog.php some rules work but some rule with multiple parameter with blog.php doesn't work.
Any help is really appreciated
You can use this rule to route localhost/apnaujjain/1/blog to http://localhost/apnaujjain/blog_detail.php?post_id=1&action=blog:
RewriteCond %{THE_REQUEST} /blog_detail\.php\?post_id(\d+)&action=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2.html? [R=302,L,NE]
RewriteRule ^(\d+)/(blog)\.html$ blog_detail.php?post_id=$1&action=$2 [L,QSA,NC]

url redirection issue using .htaccess

Below are the things i need to convert using .htaccess
If url is sample.xxx.com means i need to convert it to www.xxx.com/domain/sample
I url is sample.xxx.com/category/34/electonics.html means i need to convert it to www.xxx.com/domain/sample/category/34/electronics.html
3.After converting those things again i need to build request to the necessary pages
I have the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [P,QSA]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/news/([a-zA-Z0-9_\-]+)/([0-9]+)_([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/category/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ category.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/gallery/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ gallery.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/video/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ video.php?domain=$1&category=$2 [Nc,L]
But it fails
If i change line 4 to
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [NC,QSA]
means it will work but the browser shows the reconstructed url.
Anyone spot my error please.
If you keep http:// in target URI then it will indeed be full redirect.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^((?!domain/).*)$ domain/%1/$1 [L,NC]
RewriteRule ^domain/([\w-]+)/news/([\w-]+)/([0-9]+)_([0-9]+)/([\w-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [NC,L]
RewriteRule ^domain/([\w-]+)/category/([0-9]+)/([\w-]+)\.html$ category.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/gallery/([0-9]+)/([\w-]+)\.html$ gallery.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/video/([0-9]+)/([\w-]+)\.html$ video.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/(.+)/?$ index.php?domain=$1 [NC,L]

How to do Url rewriting for folders?

I need to rewrite the link below
www.website.com/folder1/folder2/index.php?name=value
to be like this :
www.website.com/folder1/folder2/name/
I tried htaccess with this line of code, but it doesn't work.
RewriteEngine on
RewriteRule ^name/([A-Za-z0-9-]+)/?$ index.php?name=$1 [NC,L]
Thank you in advance!
Insert this rule in /folder1/folder2/.htaccess:
RewriteEngine On
RewriteBase /folder1/folder2/
RewriteCond %{THE_REQUEST} /index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ name/%1? [R=302,L,NE]
RewriteRule ^name/([A-Za-z0-9-]+)/?$ index.php?name=$1 [NC,QSA,L]

.htaccess redirecting to 404 error

Problem:
I want to redirect from www.domain.com/index.php?mod=daily to www.domain.com/daily_rent
While redirecting from www.domain.com/advert.php?id=11 to www.domain.com/advert/11 works fine like this:
RewriteCond %{THE_REQUEST} advert\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^advert\.php$ http://www.domain.com/advert/%1? [R=301,L]
the same rule
RewriteCond %{THE_REQUEST} index\.php\?mod=daily\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/daily_rent? [R=301,L]
redirects to www.domain.com/daily_rent and throws 404 error. What could be the reason?
UPD:
Redirecting from www.domain.com/index.php?mod=daily to www.domain.com/daily_rent works fine, thanks anubhava
In that case another problem, on similar pages i have more than one query params, like this(example):
www.domain.com/?mod=daily&room=1&area=bstn
so, i try use that rule
RewriteCond %{THE_REQUEST} \?mod=daily&room=([0-9]+)&area=([A-Z]+)\ HTTP/
RewriteRule ^$ http://www.domain.com/daily_rent/room/%1/area/%2? [R=301,L]
RewriteRule ^daily_rent/room/([0-9]+)/area/([A-Z]+)/?$ /?mod=daily&room=$1&area=$2 [QSA,L]
what is the solution for that situation?
Thanks
Because you're missing internal rewrite rules. Try these rules:
RewriteCond %{THE_REQUEST} advert\.php\?id=([0-9]+)\s
RewriteRule ^advert\.php$ /advert/%1? [R=301,L]
RewriteCond %{THE_REQUEST} index\.php\?mod=(daily)\s
RewriteRule ^index\.php$ /%1_rent? [R=301,L]
RewriteRule ^(daily)_rent/?$ /index.php?mod=$1 [QSA,L]
RewriteRule ^advert/([^/]+)/?$ /advert.php?id=$1 [QSA,L]
Surely you want it the other way round - to redirect /daily_rent to /index.php?mod=daily? That would be a more common usage or mod_rewrite.
RewriteRule ^daily_rent$ /index.php?mod=daily [R=301,L]
You don't need the RewriteCond

How to delete a subdirectory from a URL?

So I'm trying to turn my website from this:
http://profe5.com/Profe5-Web/public_html/login.html
To this:
http://profe5.com/login
I've been struggling to do this, but whenever I run it I get 404 error!
This is my htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [R=301,L]
RewriteRule ^Profe5-Web/public_html/(.*)$ $1 [R=301,L]
It would be so awesome if you guys could help me!
Thanks so much!
This should be your complete .htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Profe5-Web/public_html/([^.]+)\.html [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ Profe5-Web/public_html/$1.html [L,QSA]
you might consider instead doing this via a virtual host; check with your hosting company about setting it up
If you want /login to map to Profe5-Web/public_html/login.html - try this:
RewriteRule ^([^.]+)$ Profe5-Web/public_html/$1.html [R=301,L]
The RewriteRule you already have, RewriteRule ^([^\.]+)$ $1.html [R=301,L] will map /login to /login.html, which doesn't seem to do what you need it to.

Categories