2

Can anyone help me to remove query string from URL using .htaccess

Current URL

xyz.com/blog-detail?slug=slugvalue

I want this type of url

xyz.com/blog-detail/slugvalue

Basically I want slug value in URL and remove parameter from URL

Here is my code:

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
<ifmodule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</ifmodule>


# BEGIN Expire headers  
 <IfModule mod_expires.c>  
 #  # Turn on the module.
  ExpiresActive on
  # Set the default expiry times.
  ExpiresDefault "access plus 2 days"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/svg+xml "access 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
  ExpiresByType image/ico "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"
  ExpiresByType text/html "access plus 600 seconds"
</IfModule>  

here is my all htaccess file code

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]

please check all above code of my file

please help me to solve my issue

9
  • use RewriteRule with mod_rewrite enabled Commented Feb 10, 2022 at 9:16
  • I have created this code but i think it's not work with whole website Commented Feb 10, 2022 at 9:24
  • RewriteEngine on RewriteCond %{QUERY_STRING} ^slug\=slugvalue$ RewriteRule ^blog\-detail$ example.com/blog-detail/slugvalue? [R=301,L] Commented Feb 10, 2022 at 9:25
  • here i want automatic detect slug value Commented Feb 10, 2022 at 9:34
  • @KunalVerma, Thanks for sharing your efforts, could you please also mention sample URL which you are hitting in browser? Commented Feb 10, 2022 at 9:43

1 Answer 1

1

With your shown samples, attempts please try following htaccess rules. Please make sure to place these rules at the top of your htaccess file.

Also make sure to clear your browser cache before testing your URLs.

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]

##Internal rewrite to php file for frontend request:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$  blog-detail.php?slug=$1 [QSA,L]

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.

18 Comments

this code conduct a error of 500
@KunalVerma, do you have any other rules apart from these?
Yes i have here is my htaccess file please check and help me to solve this problem https : // we.tl/t-xErHcqk1RC
@KunalVerma, please don't post links, rather post complete htacess in your question itself and let me know then.
Thanks alot sir you have solved my big problem
|

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.