i am struggling to get my site htaccess work... but no luck..
below is the code
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/? /profile.php?username=$1 [L,NC]
</IfModule>
this works fine no issues.. but when i add something it will not work.. in following cases -
1) For example when i type example.com/dinesh it will redirect me to www.example.com (always home page instead i want it to be www.example.com/dinesh
2) now i have another users.php with two parameters the thing is when i pass one parameter it should execute this rule profile.php but when i pass two parameters then it should take me to user.php i tried so many combination but this is not working.
if any expert can give me atleast some tips that will be great.
As for 1), it probably doesn't have anything to do with the above rules. If /dinesh exists, you should look in there. If it doesn't exist, you should look into /profile.php, that's probably what's redirecting you to the home page.
As for 2), your rule:
RewriteRule ^([^/]+)/? /profile.php?username=$1 [L,NC]
Matches the URI: /something/else, because the regular expression doesn't have an end-of-string match. It matches the first something, and that's good enough. If you add a $ to the end, it won't match /something/else.
RewriteRule ^([^/]+)/?$ /profile.php?username=$1 [L,NC]
Or, you can place the other rule that routes to user.php before the one that routes to profile. but it's better to have the $.
Related
I have the following URL
https://example.com/expert-profile?id=john-doe&locale=en
I want it to be redirected to
https://example.com/expert/john-doe
I tried the following
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([^&]+)&?(.*)?$
RewriteRule ^expert-profile$ https://example.com/expert/%3?%1%4 [L,R=301]
And a couple of other solutions, nothing is working here. Can someone help me to go in the right direction?
Update:
This is my current .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
Redirect 301 "/en/download-app" "/download-app"
Please keep your htaccess file in your root and have it in following way.
Please clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^id=([^&]*)&locale=(.*)$ [NC]
RewriteRule ^([^-]*)-.*/?$ $1/%1-%2 [R=301,L]
OR in case you don't have Rules to handle non-existing files/directories then use following Rules set. Please make sure either use above OR following Rules set one at a time only.
RewriteEngine ON
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{QUERY_STRING} ^id=([^&]*)&locale=(.*)$ [NC]
RewriteRule ^([^-]*)-.*/?$ $1/%1-%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:expert)/([^-]*)-(.*)$ $1-profile?id=$1&locale=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ /index.html [L]
I have following URL
https://example.com/expert-profile?id=john-doe&locale=en
I want it to be redirected to
https://example.com/expert/john-doe
You would need to do something like the following at the top of your .htaccess file, before your existing directives (order is important):
RewriteCond %{QUERY_STRING} (?:^|&)id=([^&]+)
RewriteRule ^expert-profile$ /expert/%1 [QSD,R=301,L]
This captures the value of the id URL parameter (in the %1 backreference) regardless of where it appears in the query string and discards all other URL parameters. I'm assuming you don't specifically need to match locale=en?
Note that the regex subpattern ([^&]+) (the id value) only matches something, not nothing. If the URL parameter is empty (ie. id=&locale=en) then no redirect occurs.
The QSD flag is necessary to discard the original query string.
Test first with a 302 (temporary) redirect to avoid potential caching issues. And clear your browser cache before testing. Only use a 301 (permanent) redirect if this really is intended to be permanent.
To redirect the specific URL /expert-profile?id=<name>&locale=en to /expert/<name>, ie. the id parameter is at the start of the query string and is followed by locale=en only then you can (and should) be more specific in the condition. For example:
RewriteCond %{QUERY_STRING} ^id=([^&]+)&locale=en$
RewriteRule ^expert-profile$ /expert/%1 [QSD,R=301,L]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([^&]+)&?(.*)?$
RewriteRule ^expert-profile$ https://example.com/expert/%3?%1%4 [L,R=301]
This is close (providing you placed the rule at the top of the file), however, this tries to preserve the other URL parameters, ie. locale=en and whatever else, to create another query string - which you've not stated in your requirements.
Aside: The existing answers are assuming you are wanting to internally rewrite (URL rewrite) the request in the other direction, ie. from /expert/john-doe to /expert-profile?id=john-doe&locale=en. This is probably due to how questions of this nature are notoriously miswritten and this is often the real underlying intention. However, you've made no mention of this here and a URL of the form /expert-profile is not a valid endpoint - so it wouldn't really make sense to "rewrite" the URL in that direction. (?)
If you want it rewritten, capture the name (.+) and insert it into the target $1
RewriteRule ^expert/(.+)$ /expert-profile?id=$1&locale=en [L]
And don't use flag R|redirect here, unless you really want a redirect.---
To redirect from expert-profile?id=john-doe to expert/john-doe, capture the id (.+?) from the query string and insert it in the substitution URL %1
RewriteCond &%{QUERY_STRING}& &id=(.+?)&
RewriteRule ^expert-profile$ /expert/%1 [R,L]
When everything works as it should, you may replace R with R=301 (permanent redirect).
Don't use both rules together. If you do, it will result in an endless redirect loop and finally give a "500 Internal Server Error".
Unrelated, but never test with R=301!
I'm trying to rewrite my URLs with unique slug like Instagram or Facebook.
Example: facebook.com/joe
My URLs are like that: website.com/user.php?username=joe
I try this rule in .htaccess:
RewriteEngine On
RewriteRule ^([a-z]+)$ user.php?username=$1 [L]
But it doesn't work, it redirects on /user.php.
It works if I use this rule:
RewriteEngine On
RewriteRule ^u/([^/]*)$ /user.php?username=$1 [L]
But result is website.com/u/joe and I prefer without u.
Any idea?
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?username=$1 [NC,L]
The .* in the pattern ^(.*)$ matches /anything and the parentheses help capture the anything part as a $1 variable used in the substitution URL as user.php?username=$1.
In case if you need multiple parameters you can simply add &%{QUERY_STRING} after $1 to separate and add them to the end of your query string.
for example : if you pass website.com/joe?age=31 the result will be website.com/user.php?username=joe&age=31.
Finally, the flag NC simply makes the rule non case-sensitive, so it matches /Joe or /JOE as well.
On a site I'm working on, if you enter the url, plus 1 directory, the htaccess adds a trailing slash.
So, this: http://www.mysite.com/shirts
Becomes this: http://www.mysite.com/shirts/
The htaccess that runs the site is quite long and complex, so it's not easy to find or test which rule is causing the rewrite. I was able to track down the issue to this line of code (I think):
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Does this rule match the behavior I'm describing above? It seems to be the cause, but it doesn't make logical sense to me. I don't unsderstand where the trailing slash is coming from.
Can someone shed some light on this for me? Thanks in advance.
Edit: MORE:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
By default apache will add the ending /, you will have to use:
DirectorySlash Off
To disable that behavior which is caused by mod_dir, you can read more about it here.
However if you're trying to remove the / to fix images not showing. That is not the right way to do it, you should instead use the HTML base tag, for example:
<BASE href="http://www.yourdomain.com/">
Read more here about it.
Your current rule as you have updated on your question:
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Means:
if domain on the URL is only mysite.com
redirect current URL to domain with www.
So an example of it would be, if you access:
http://domain.com/blog/some_blog_article
It will redirect the user to:
http://www.domain.com/blog/some_blog_article
Note how it retains everything and only add the www. to the domain.
If you really want to redirect it regardless here is one way to do it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
# check if it is a directory
RewriteCond %{REQUEST_FILENAME} -d
# check if the ending `/` is missing and redirect with slash
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
# if file or directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# and we still want to append the `/` at the end
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
I'm trying to rewrite some parameters to beautiful links, but for a subdomain / a folder only. Unfortunately I can't get it to work, maybe also because there are some other rewrites in line before...
Heres my code:
<IfModule mod_rewrite.c>
# NON-WWW TO WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# WORDPRESS-BLOG
Options +FollowSymlinks
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# REDIRECT FOR SUBDOMAIN
RewriteCond %{HTTP_HOST} ^subdomain.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ index.php?cshort=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /index.php?cshort=$1&cid=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /index.php?cshort=$1&cid=$2&step=$3 [L]
</IfModule>
Basically only the last part is the one I want to rewrite to change URLs from something like
http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1
The other rewriting rules for www.example.com shouldn't get affected. Unfortunately my current codes only does the first two rules for the blog and the www, but nothing happens on the subdomain. What's wrong in my code?
When you say that you want to rewrite from http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1 you mean that you want the user to enter the pretty URL and to have it serve the full URL in the background, not that you want to redirect from the ugly to the pretty URL, right?
In your RewriteRules, what are you trying to accomplish with "(?:/)?"? As written, that doesn't make any sense to me. If you're just trying to match whether or not the directory path ends with a slash, you can do that as follows:
RewriteRule ^([^/.]+)/?$ index.php?cshort=$1 [L]
EDIT: Additional suggestions:
Move the "Redirect for subdomain" section above the "Wordpress Blog" section. Since the Wordpress rule applies to "everything that's not a real file or directory, regardless of domain" that should go last.
RewriteConds only apply to a single RewriteRule that follows them. For each of the three rules you have listed under "Redirect for subdomain", after updating them per the above suggestion, you need to repeat the two RewriteCond lines in front of the RewriteRule.
How can I allow visitors to use this link (www.example.com/news?id=34) in order to see (www.example.com/index.php?page=news&id=34)
Right now I am hiding the extensions of my PHP files in order to make the links look nice. When visitors go to (www.example.com/news) they can see the page (www.example.com/index.php?page=news). However, when they go to (www.example.com/news?id=12) they get a 404 error.
At this point, PHP does not recognize the id parameter. Here is what I currently have in my .htaccess file
Options +FollowSymlinks
RewriteEngine on
# catch request with no querystring like /Home
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
# make other requests with a non-empty query string go to /index.php?id=XXXXX
RewriteCond %{QUERY_STRING} ^.*$
RewriteRule ^$ /index.php?id=$1 [QSA,L]
Your second test pattern (^$) only matches the case where the user doesn't put any path information in, but does include a query string (e.g. www.example.com/?id=12). Also note that the backreference $1 has no value in that rule either.
The simplest solution is to just combine the two rules, since you can use the QSA flag to do the work of appending the id=# part for you:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [QSA,L]
The condition of your first rule fails as the query is not empty. Try it without that condition but with the following condition instead:
RewriteCond $1 !=index.php
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]