htaccess change url strings to simple slug - .htaccess

This is my current url
https://www.example.com/content.php?q=1&s=title-sample-slug
I need to change above url using this type url
https://www.example.com/title-sample-slug
how can i do it using htaccess file. Need to hide the content.php?q=1&s= from url in the browser.

RewriteCond %{QUERY_STRING} q=1&s=(.+)
RewriteRule content.php %1 [L]

This is correct answer for my question
RewriteCond %{QUERY_STRING} q=([0-9a-zA-Z_-]+)&s=([0-9a-zA-Z_-]+)
RewriteRule content.php %2 [L,QSD]

Related

Redirect querystring with ? in it using .htaccess

I have a URL format of
http://www.domain.com/?text
For any instance of this URL I'd like to redirect to a 3rd party URL but can't figure out how to match the supposed querystring.
RewriteCond %{QUERY_STRING} ^/text(.*)$
RewriteRule http://www.google.com [R=301,L]
Doesn't work
Your attempt is fairly close. Add /? in target to strip off this query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^/qipco
RewriteRule ^ http://www.google.com/? [R=301,L]

Redirect this dynamic URL to html URL with htaccess

I have lot or articles with this URL format:
mydomain/art/details.php?articleid=24463&parentid=1&catid=166
the above is an example.
And i want that redirect these kind of URL to :
mydomain/art/24463.html
Please let me know that how can i do it with .htaccess?
Thanks in advance
Instead of mydomain/art/24463.html you should choose something like mydomain/art-166/1-24463.html.
This way, you'll include also parentid and catid which are dynamic parameters (and so, needed for rewriting).
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/art/details\.php\?articleid=(\d+)&parentid=(\d+)&catid=(\d+)\s [NC]
RewriteRule ^ /art-%3/%2-%1.html? [R=301,L]
RewriteRule ^art-(\d+)/(\d+)-(\d+)\.html$ /art/details.php?articleid=$3&parentid=$2&catid=$1 [L]
Try this in htaccess tester
RewriteEngine on
RewriteRule ^art/(\d+).html$ art/details.php?articleid=$1&parentid=1&catid=166 [nc]

htaccess redirect root?var=abc to file.php?var=abc

I'm having trouble redirecting URLs like site.com/?var=abc to site.com/file.php?var=abc.
I tried a lot of redirect codes but none worked. I installed Wordpress in the root directory of the site and I'm trying to move the old content based on GET variables to file.php.
Thank you!
To redirect every empty url to file.php, here is what you need to write in your .htaccess:
RewriteRule ^/?$ file.php [R=301,QSA,L]
[QSA] tag will append your query string (?var=abc) to the new url.
To redirect the exact format /?var=abc, you'll need to use %{QUERY_STRING}:
# Check for query string, must be exactly "?var=abc"
RewriteCond %{QUERY_STRING} ^var=abc$
# Check for url, must be "/" or ""
RewriteRule ^/?$ file.php [R=301,QSA,L]
Solved by adding (.*)$ after the first variable (var), there were many other parameters in the link.
RewriteCond %{QUERY_STRING} ^var=(.*)$
RewriteRule ^/?$ /file.php$1 [R=301,QSA,L]

Passing a Query String via .HTACCESS

I have a dynamic query string that I need to pass via an .htaccess redirect. For example:
I need to redirect this URL: http://mysite.com/page1?action=signup&var2=dynamicVar
To this: http://mysite.com?action=signup&var2=dynamicVar
I know this is pretty simple, but I'm really not sure what type of rule/syntax would work for this.
Any help is greatly appreciated!
If you already have .htaccess then simply add this line:
RewriteRule ^page1/?$ page2 [L,R,QSA,NC]
Update: Based on your comments:
RewriteCond %{QUERY_STRING} (^|&)action=signup(&|$) [NC]
RewriteRule ^page1/?$ / [L,R,QSA,NC]
RewriteRule ^page1(.*)$ /page2$1 [L,R=301]
Input
http://mysite.com/page1?action=signup&var2=dynamicVar
this is the rewrite rule
RewriteRule ^page1?(.*)$ /?$1 [L,R=301]
redirect to
http://mysite.com/?action=signup&var2=dynamicVar
this will redirect all request o page1 with get parameters to http://mysite.com
Use %{REQUEST_URI}
fi.:
RewriteCond %{HTTP_HOST} ^website\.(.+)$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
rewrites anything that starts with "website" to "www.website..." including the querystring (by use of %{REQUEST_URI})

htaccess original rewrites already used

Ok so I have a website that already takes the SEO'd URLs and parses the info so that I can get the content for them on their respective pages.
the code in the htaccess file looks like this:
RewriteCond %{REQUEST_URI} !^/(email)/
RewriteRule ^([A-Za-z0-9-]+)\.html /page.php?seo_title=$1 [L]
RewriteCond %{REQUEST_URI} !^/(email)/
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)\.html /page.php?seo_title=$2&seo_parent=$1 [L]
So now a couple of pages have changed from:
old-parent-name/seo-title.html to new-parent-name/seo-title.html
but when i try to do any type of regular 301 redirect, the URL string ends up like this:
http://www.example.com/new-parent/old-seo-title.html?seo_title=old-seo-title&seo_parent=new-parent
Is there any way to do this so that the Query String does not appear?
thanks in advance.
Try adding this above the rules that you have:
RewriteCond %{REQUEST_URI} !^/(email)/
RewriteRule ^old-parent-name/([A-Za-z0-9-]+)\.html /new-seo-parent/$1.html [L,R=301]

Resources