2

Once my friend help me to solve this issue, I have written code like that. Where example.com/blog-detail?slug=slugvalue it goes to blog-detail.php and this url example.com/project?slug=test goes to project.php.

RewriteEngine ON
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$  blog-detail.php?slug=$1 [QSA,L]

I think it's work for only blog-detail page now i face the issue with this url https://www.example.com/project?slug=test, it redirect me to 404 page, could you help me here.

2
  • Thanks for sharing efforts, could you please do mention which request should go to blog.php? and which request should go to other php file? Commented Feb 10, 2022 at 13:45
  • @RavinderSingh13 example.com/blog-detail?slug=slugvalue it goes to blog-detail.php and this url example.com/project?slug=test goes to project.php Commented Feb 10, 2022 at 13:55

1 Answer 1

2

With your shown samples, attempts; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

I have added 2 solutions here, so either use 1st rules set OR use 2nd rules set ONLY ONE at a time please.

1st solution: With 2 different rules set for each php file please try following rules.

RewriteEngine ON
###Rules for blog-detail.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(blog-detail)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$  blog-detail.php?slug=$1 [QSA,L]

###Rules for project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(project)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$  project.php?slug=$1 [QSA,L]


2nd solution: A Generic solution with capturing groups to deal with both kind of php files together.

RewriteEngine ON
###Rules for blog-detail.php OR project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$  /$1.php?slug=$2 [QSA,L]

NOTE1: Please keep both your php files(project.php and blog-detail.php) should be kept along with your htaccess rules file.

NOTE2: For JS/CS rewrite/redirect: You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

Sign up to request clarification or add additional context in comments.

28 Comments

First solution is only work for blog-details page not working for project and if i use second solution then both url goes to 404 page
@KunalVerma, please check my 2nd solution once now, I edited it now, there was a typo, let me know how it goes. Make sure both php files are residing along with your htaccess file.
Now page is working fine but redirection not apply
both url open with slug
I have update the second solution please check and approve it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.