Need to write multiple htaccess rule with multiple parameters - php

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]

Related

HTACCESS - mod_rewrite

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]

.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

Two redirect rules conflict with each other .htaccess

I had problems with a .htaccess file, that has been fixed, now i have another problem. I wish to clean the url using a .htaccess file. here is my current rule
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=page&b=([^&]+)[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=page&b=$1 [L,NC,QSA]
This will clean page links to point to a clean url that have a link containing a=page. It works perfectly. I tried to add another rule so that it applies to links containing a=rpofile but it did not work. However it does work if i remove the above code. It seems that i can not have both rules running at the same time. Below is the a=profile version of the above code.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=profile&u=([^&]+)[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=profile&u=$1 [L,NC,QSA]
If URL is /about then first rule will forward to /index.php?a=page&b=about now same /about cannot be forwarded to /index.php?a=profile&b=about.
But you can have /profile/something be treated as profile URL.
You can have 2 rules as:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=page&b=([^\s&]+)[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ /index.php?a=page&b=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=profile&u=([^\s&]+)[\s&] [NC]
RewriteRule ^ /profile/%1? [R=301,L]
RewriteRule ^profile/([^/.]+)/?$ /index.php?a=profile&u=$1 [L,NC,QSA]

Simple mod_rewrite rule not working [L]

So this is my first time doing mod_rewrite and I'm using tutorials and generators and it's been going okay so far except for the last line below which if I haven't made any mistake should redirect about.php to about/
Anybody know why it doesn't ?
All the other rules are working without issues (I did double check the page existed, I tried to add a slash before about.php as my website is on a server with multiple websites...)
Options +FollowSymLinks
#Sub-dir e.g: /cmsms
RewriteBase /
#redirect www.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.metareviewr.com
RewriteRule (.*) http://metareviewr.com/$1 [R=301,L]
#RewriteRule ^show/([^/]*)/$ /tvshow.php?id=$1 [L]
RewriteRule ^show/([^/]*)/([^/]*)/$ /tvshow.php?id=$1&?name=$2 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://metareviewr.com/$1 [R=301,L]
RewriteRule ^about/$ about.php [L]
This should work
RewriteRule ^about.php$ about/ [L,R=301]

Simple htaccess rewrite/redirect to search page

I have a search form that sends a GET request to a page called search.php. I have rewrite rules set up in my htaccess file that rewrite certain things, like /search, to their respective pages. I simply want to take the search.php?q=query and rewrite it to /search/query.
Here is what I have.
RewriteRule search.php?q=(.*) /search/$1
RewriteRule search/(.*) search.php?q=$1 [nc]
What am I doing wrong?!
Here is the complete file
ErrorDocument 404 /index.php?p=404
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wghandcrafted.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|psd|js|swf|flv|png)$ /feed/ [R=302]
RewriteRule ^(products|blog|feed|search|checkout|checkout)$ $1.php [nc]
RewriteRule products/cat/(.*)$ products.php?type=cat&cat=$1 [nc]
RewriteRule products/(.*)$ products.php?type=single&product=$1 [nc]
RewriteRule blog/(.*) blog.php&post=$1 [nc]
RewriteRule feed/(.*) feed.phptype=$1 [nc]
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
Make the first line perform a Redirect and the second perform a Rewrite
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
and move
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
to the end of the set of rules.
Otherwise, the RewriteCond %{REQUEST_FILENAME} !-f rule is enacted before anything else, meaning that only requests for non-existant files will be handled by any rules below that line. As there is a search.php file, this prevents that rule from ever being reached.
I was having the same problem and here is a solution i found that worked for me, on my site the queries are being sent to index.php, I discovered if I had "^index.php$ /search/%1? [R=301]" as the first rewrite rule it will just error out because of the second rewrite rule making it just go in a loop so i replaced "^index.php$" with "^$" allowing it to still request the same file. It might not be the best solution, but a work around that works. Here is my working code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^$ /search/%1? [R=301]
RewriteBase /search
RewriteRule ^search/(.*)$ /index.php?q=$1 [NC]

Categories