简体   繁体   中英

.htaccess rewrite redirect

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

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]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM