I have url's like following with query string. I want to remove query string from url and want to make clean url.
www.demo.com/following.php?user=hardik
www.demo.com/fans.php?user=john
This url's should be like
www.demo.com/hardik/following
www.demo.com/john/fans
OR should be like
www.demo.com/following/hardik
www.demo.com/fans/john
Is this possible with htaccess? I tried to find a loot in google but still no luck. Need help.
Update:
I need something like this
www.demo.com/user/hardik/following/
www.demo.com/user/john/fans/
I tried like this
RewriteRule ^user/([^?]*) following.php?user=$2/ [L,QSA]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(\w+)/(\w+)/?$ $2.php?user=$1 [L,QSA,NC]
Try with this for a url like www.demo.com/following/hardik
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z_-]+)/([a-zA-Z_-]+)/?$ $1.php?user=$2
Finally this is working for me for my last requirement. May be help someone else.
RewriteEngine On
RewriteBase /
RewriteRule ^user/([^*]+)/following/$ following.php?user=$1 [L,QSA]
RewriteRule ^user/([^*]+)/fans/$ fans.php?user=$1 [L,QSA]
Related
Sorry, this is probably a simple issue but I've read tons of tutorials and can't solve the issue. Here's the URL sample:
http://localhost:8106/privacy-policy/?lang=fr&dest=app
The .htaccess contents:
RewriteEngine on
RewriteRule ^privacy-policy/?lang=([a-z][a-z])&dest=app$ privacy-policy/$1 [NC,L]
When I visit the URL I don't get redirected. Any ideas?
Thanks
You can't match against the query string in a rule, you need to use the %{QUERY_STRING} variable:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=([a-z][a-z])&dest=app$
RewriteRule ^privacy-policy/$ privacy-policy/%1 [NC,L]
Note that the backreference needs to be %1. If you need it to redirect the browser, you'll also need a R flag in the square brackets.
Thanks Jon Lin for the leads on the query_string and %1 edit. It didn't end up working with a copy/paste of that code but this is finally what I ended up with and it is working:
RewriteCond %{REQUEST_URI} privacy-policy
RewriteCond %{QUERY_STRING} lang=(\w+)&dest=app
RewriteRule ^privacy-policy/$ /privacy-policy/%1? [R=301,L]
Thanks again for your help on this.
Disclaimer : I dont know anything about URL rewriting
currently my URL looks like
legalHQWithNewAddressTable/legalHQ/public/admin/Parties.php?caseid=7 (any number basically)
But how can I make it to looks like the following
legalHQWithNewAddressTable/legalHQ/public/admin/Parties/caseid/7/
I'm trying but not working RewriteRule ^([a-zA-Z0-9]+)/?
Please dont make things complicated I dont know anything about URL rewriting so please keep it as simple as possible.
Any Idea?
Try adding these rules to the htaccess file in the /legalHQWithNewAddressTable/legalHQ/public/ folder:
RewriteEngine On
RewriteBase /legalHQWithNewAddressTable/legalHQ/public/
RewriteCond %{THE_REQUEST} /admin/([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ admin/%1/caseid/%2/?%3 [L,R]
RewriteRule ^admin/([A-Za-z0-9]+)/caseid/([0-9]+)/ admin/$1.php?caseid=$2 [L,QSA]
Place this rule in your /legalHQWithNewAddressTable/legalHQ/public/admin/.htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /legalHQWithNewAddressTable/legalHQ/public/admin/
RewriteRule ^Parties/caseid/([0-9]+)/?$ Parties.php?caseid=$1 [L,QSA,NC]
Trying to make a redirection URL in htaccess.
I want to redirect URLs like
www.domain.com/pageANYTHING
to
www.domain.com
But I have an exception : when I got this
www.domain.com/page.phpANYTHING
do nothing.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^\.phpANYTHING [NC]
RewriteRule ANYTHING$ http://www.domain.com/? [L,R,NC]
Try this:
RewriteEngine On
RewriteRule ^page(?!\.php).*$ / [R=301,L]
In fact It works with a mix of your two propositions.
RewriteCond %{REQUEST_URI} !\.(?:php)$
RewriteRule ^page(?!\.php).*$ / [R=301,L]
Thank you so much guys.
I am trying to configure a dynamic mod rewrite rule in my .htaccess file using mod_rewrite. I am very close to figuring this out. I am trying to get URLs like these:
http://www.mysite.com/index.php?service=14&title=events
http://www.mysite.com/index.php?service=48&title=planning
To automatically be rewritten to these:
http://www.mysite.com/service/14/events
http://www.mysite.com/service/48/planning
Here is my codes so far:
RewriteRule ^service/(.*)/(.*)$ index.php?service=$1&title=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^service=$1&title=$2 [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php [NC]
RewriteRule ^index.php$ /service/$1/$2 [L,R=301]
I think there is something wrong with the last line maybe? I'm not the best at regular expressions so any help would be greatly appreciated.
Edit: Just wanted to be clear that the pretty URLs do work. However, the old URLs aren't redirecting and are still displaying in the browser.
I am not sure if this would be any easier but if I could get the URLs to look like this:
http://www.mysite.com/service/14/title/events
http://www.mysite.com/service/48/title/planning
Then that would work too. I don't really need the second query title to be in the URL but if it's easier to leave it in there, then no big deal.
Edit: Answered
Many thanks to all who helped contribute to the solution. I got this for my rewrite rule:
RewriteRule ^index/service/(.*)/(.*)/$ index.php?service=$1&title=$2
Once I added 'index' it worked with both query strings. As far as the redirect goes, I edited my php script and did all the URL redirecting there, which was a lot easier. Special thanks to mkjasinski for pointing that out.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^service/([0-9]+?)?/([a-zA-Z\-]+?)$ index.php?service=$1&title=$2 [L,NC]
and if I have run: http://localhost/service/12/This-is-text in $_GET in index.php:
array (size=2)
'service' => string '12' (length=2)
'title' => string 'This-is-text' (length=12)
you can't use special holders ($1,$2...) from previous rewrite rules into rewritecond.
change your code to:
RewriteRule ^service/(.*)/(.*)$ index.php?service=$1&title=$2 [NC,L]
RewriteCond %{QUERY_STRING} (?:^|&)service=([0-9]+)(&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)title=([a-z]+)(&|$) [NC]
RewriteRule ^index.php$ /service/%1/%3? [L,R=301]
I have a tricky issue redirecting some URLs internally for my site.
The situation is this, I currently have a URL like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]
RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]
However I would also like to be able to redirect to check.php using a URL like example.com/youtube.com. Unfortunately it is just beyond me to figure it out.
I have a directory /assets/ with all the CSS, JS, etc. which shouldn't be affected.
Thanks
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]
This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.
As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?