Using htaccess how to turn this type of URL:
http://www.mysite.com/page.php?slug=hello-world
into this type:
http://www.mysite.com/page/hello-world
And not just this one url but all urls in the 1st format to the 2nd format.
If the second one is the URL you want people to see, use this:
RewriteRule ^page/(.*)$ /page.php?slug=$1
If it's the other way around:
RewriteRule ^page\.php?slug=(.*)$ /page/$1
EDIT: Also, make sure you have the following in your .htaccess before ANY RewriteRules:
RewriteEngine On
You may want to refer to here and this
Here is my thought to your solution:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^slug=[^/]+$ [NC]
RewriteRule ^page\.php$ http://www.mysite.com/page/$1? [R=301,L]
Related
I would like to make a rewrite rule for my website but I cannot seem to get the proper code in order to make this work.
The current URL of my website is looking like http://www.mohanadarafe.io/JSON/json.html
I want it to be: http://www.mohanadarafe.io/json
I have tried the following code but it does not seem to work:
RewriteEngine On
RewriteRule ^\.html$ /json [L]
Any idea how to fix this?
You have it reversed. Have it like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /JSON/json\.html [NC]
RewriteRule ^ /json [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^json/?$ JSON/json.html [L,NC]
Then you can have your URL as: http://www.mohanadarafe.io/json
I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]
I have this url
http://example.com/profile.php?u=78
I wanted to changed it to be like this
http://example.com/78/
How would I do that?
Please Help
Thanks in advance
You can use this .htaccess in your root directory:
RewriteEngine on
RewriteRule ^(\d+)/?$ /profile.php?u=$1 [L]
RewriteCond %{QUERY_STRING} ^u=(\d+)$
RewriteRule ^profile.php$ /%1/? [R=302,L]
Change [R=302,L] to [R=301,L] when that work well
Try this to rewrite http://example.com/profile.php?u=78 into http://example.com/78/:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^profile.php$ /%2/
RewriteRule (.*) $1? [R=permanent]
Line-by-line explanation:
Turn on rewriting functionality
Specify the condition to make sure the query string is in the form of foo=bar for the following rules to apply.
Rewrite profile.php?foo=bar into /bar/?foo=bar
Remove all query strings that may still be appended to the new URL like /bar/?foo=bar/. This rule essentially substitute the entire path (.*) with itself ($1), and emptying the query string (?). [R=permanent] performs a permanent redirect (301).
Also, as previously commented, please ensure that http://example.com/78/ is a valid URL.
I need this logic to work:
I want rewrite this string for users to see
http://mysite.com/index.php?cl=mykeystring
to
http://mysite.com/otherkey/
http://mysite.com/index.php?myvar=test&cl=mykeystring&mysecondvar=morevalue
to
http://mysite.com/otherkey/myvar=test&mysecondvar=morevalue
But when http://mysite.com/otherkey/ is written, so load
http://mysite.com/index.php?cl=mykeystring, but no redirects will be done.
Is it possible? There are no possibility to change anything in codes, but only .htaccess
This logic is nearly realized by this code:
RewriteCond %{QUERY_STRING} ^(.*?)cl=mykeystring(.*?)$ [NC]
RewriteRule ^index.php$ /otherkey/%1%2? [R,L]
RewriteRule ^otherkey/(.*?)$ /index.php?cl=mykeystring&$1
but im getting some not needed amp symbols on first rewrite rule. any solutions?
I think you can do something like this:
RewriteCond %{QUERY_STRING} cl=mykeystring [NC]
RewriteRule ^index.php /otherkey/ [QSA]
Docs here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
I am facing a problem in redirecting a old url to a new one.
This is the old url: http://www.abc.com/department.asp?dept=Minimal%20cloth
the new url should be: http://www.abc.com/Minimal-cloth
Please suggest me the best possible way
Can you try following rules in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
# to take care of /department.asp?dept=Minimal%20cloth or
# /department.asp?dept=Minimal cloth tyoe of URLs
RewriteCond %{QUERY_STRING} ^dept=(.+)(\s|%20)(.+)$ [NC]
RewriteRule ^department\.asp$ /%1-%3? [R=302,L,NC]
# to take care of /department.asp?dept=MinimalCloth type URLs
RewriteCond %{QUERY_STRING} ^dept=(.+)$ [NC]
RewriteRule ^department\.asp$ /%1? [R=302,L,NC]
Remember RewriteRule doesn't match query string.
Apache's mod_rewrite is perfect for this. Excellent tutorial here.
Depending on which modules are enabled in the apache server's config, you can use either mod_alias or mod_rewrite for this. To use mod_alias, use something like
RedirectMatch 301 ^/department\.asp\?dept=(.*) /$1
To use mod_rewrite try something like:
RewriteCond %{QUERY_STRING} ^dept=(.*)$ [NC]
RewriteRule ^/department\.asp/%1 [R=301]
These haven't been tested, but hope they get you started.