I have some code which uses Rewrite engine in my .htaccess file and it specifies the directory for my SERPs. However when someone goes onto search/QUERY/ rather than search/QUERY/PAGE/ it displays a 404 error. Same with just search/.
I want it so that if someone just goes to search/QUERY/ that it redirects them to search/QUERY/1/ and for just search/ it redirects them to my homepage /. I have included a copy of my code below.
RewriteEngine on
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&category=web&d=$2
Can anyone help me with this problem?
Thanks in advance, Callum
Try this code in your htaccess :
RewriteEngine on
RewriteRule ^search/?$ / [R=301,L]
RewriteRule ^search/([^/]+)/?$ /search/$1/1/ [R=301,L]
RewriteRule ^search/([^/]+)/([0-9]+)/?$ search.php?q=$1&category=web&d=$2 [NC,L]
You can create multiple RewriteRule statements to accomplish this. (Make sure to omit the [R] flag on intermediate rules if you use it!)
The rule you posted will only rewrite if it's of the form /search/query/n/, so write a regular expression matching search/query that redirects to /search/query/1. Do the same to redirect home with no query.
Try these additional 2 rules in your .htaccess file:
RewriteRule ^search/([^/]+)/$ /search/$1/1/ [R=301,L]
RewriteRule ^search/?$ / [R=301,L]
Related
I don't have an idea how can I remove %2523 from URL made by GET in php. I want to redirect users to page without that. Sometimes it has # on string beggining, and that's why it generates that "%2523".
For example, want to redirect them from something like:
localhost/catalog/value/%2523string
to
localhost/catalog/value/string
My current .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/catalog/value\.php\?value=([^/]*)\s [NC]
RewriteRule ^ /catalog/value/%1? [R=301,L]
RewriteRule ^catalog/([^/]*)$ /value/value.php?color=$1 [L]
Hopefully you can help me with that, trying from yesterday and still didn't nothing works.
Finally i solved that by adding this at the .htaccess beggining:
RewriteRule ^([^%]*)\%23(.*)$ /catalog/value/$2 [R=301,L]
I am trying to have it so foo.com/userinfo.php?user="username" will be re-written as foo.com/username. I have gotten this to work using RewriteRule ^([a-zA-Z0-9_-]+)$ userinfo.php?user=$1 in my htaccess. However, when a slash is added to the end of the username such as foo.com/username/ it just redirects it to foo.com/userinfo.php?user="username". While this does pull up the profile it doesn't look as well in the url bar. Thank you for any help!
Here is my .htaccess code now
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ userinfo.php?user=$1
You can use `/?' as the expression "the character / or nothing".
RewriteRule ^([a-zA-Z0-9_-]+)/?$ userinfo.php?user=$1
I have problem to redirect page in paging system. I have write .htaccess as follows.
RewriteRule ^news.html?page=2 allnews.php?type=1&page2 [NC]
RewriteRule ^news.html allnews.php?type=1 [NC]
Actually I want to add paging system in the news.html. But it is not working :(
Please help me.
Probably final URI should be allnews.php?type=1&page=2 instead of allnews.php?type=1&page2.
RewriteEngine On
RewriteRule ^news\.html$ /allnews.php?type=1 [NC,QSA]
i have this rewrite rules:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/ [NC]
RewriteRule ^.*$ - [R=404,L]
RewriteRule ^(home|contact|about|submit|search)/?$ /index.php?page=$1 [L]
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /index.php?page=home&cat=$1&slug=$2 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=home&cat=$1 [L]
What i want is:
if user types something which is not matching last three conditions i want to show a 404 error. At present it loads home page. So i am check $_GET['page'] in there. I donot want to check in php.
do you have anything to improve my rewrite rules or any additions?
please help
Since everything is ultimately routing to index.php (Front-end Controller Pattern), I suggest dropping the the mod_rewrite and using:
FallbackResource /index.php
Then parse the URL in index.php and handle routing accordingly.
This will take some rewriting, but will be more flexible in the long run.
Read more about FallbackResource and Front-Controllers in PHP.
You may use mod rewrite to add argument to your web page index.php, then treating them in the php page possibly sending back a error page (as in cakePHP). that keeps only ONE rule.
RewriteRule ^([0-9A-Za-z]+)/?$ /index.php?argument=$1 [L]
Try to use ErrorDocumentwhit location page
ErrorDocument 404 /erreur_404.html
I need make redirect from site/article to site/article/
I use this rule in .htaccess
RewriteRule ^/article(.*)$ http://balttranscom.ru/article/$1 [R=301,L]
but it's not work!
Help!
As shown at the bottom of the mod_rewrite documentation page, rules in .htaccess files aren't passed an initial slash.
RewriteRule ^article(.*)$ http://balttranscom.ru/article/$1 [R=301,L]
Do you have the following line?
RewriteEngine On