I would like to rewrite my urls from http://www.domain.com/index.php?p=activate&id=57A5dz into http://www.domain.com/users/activate/id/57A5dz.
I searched a lot around google but nothing worked.
I always see the 404 page..
My .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ \?p=$1 [QSA,L]
RewriteRule ^(.*)/(.*)/$ ?page=$1&id=$2
RewriteCond %{REQUEST_URI} ^/web-gallery/images/
RewriteRule ^images/(.+)$ /web-gallery/images/$1
And i also tried to add this:
RewriteRule ^([^/]+)/(\d+)/.*$ $1&id=$2 [L]
Thank you
First let's take a closer look at your rules to understand what they do.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ \?p=$1 [QSA,L]
If the requested url is not a directory and the requested file is not a file and it the part behind the hostname and before the query string matches ^(.*)$, internally rewrite it to whatever it was and append p= + whatever was matched to the query string. A request to example.com/cookies?are=evil would be rewritten to example.com/cookies?are=evil&p=cookies.
RewriteRule ^(.*)/(.*)/$ ?page=$1&id=$2
If the requested url contains at least 2 path segments, internally rewrite it to the same page and replace the query string with page = everything before the last path segment & id = what is behind it.
RewriteCond %{REQUEST_URI} ^/web-gallery/images/
RewriteRule ^images/(.+)$ /web-gallery/images/$1
If the requested url (or rewritten url) begins with web-gallery/images, rewrite images/something to web-gallery/images/something. There is however no url that matches both the condition and the first argument of rewriterule, so this rule is never, ever, executed.
RewriteRule ^([^/]+)/(\d+)/.*$ $1&id=$2 [L]
If the requested url has more than 2 path segments, where the first path segment doesn't contain a / and the second path segment contains only numbers, drop the rest of the path, and internally rewrite the url to whatever the first path segment was, followed by a & outside a query string and add id=numbers_matched to it. http://example.com/test/1/some/thing?cookies=evil&butter=too would be rewritten to http://example.com/test&id=1?var=2&butter=too.
To look back at what you wanted. You want the user to see http://www.domain.com/users/activate/id/57A5dz. This doesn't make sense to the server, so you'll have to internally rewrite it to let it make sense to the server. http://www.domain.com/index.php?p=activate&id=57A5dz should just do that. We want to match on the first url, and use those matches on the second url. The following (untested) rewriterule should do the trick for you. I hope above explanation gives you a better understanding of how these rules work.
RewriteRule ^users/([^/]+)/id/([^/]+)/?$ /index.php?p=$1&id=$2 [L]
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 am having trouble finding a solution for what I want to do. I am going to have a couple thounsand affiliates signing up on our site. They will provide a username, and basically give out the url www.domain.com/affiliate/username to their clients.
So, the url I will be starting with is www.domain.com/affiliate/home.html?userid=bob (if bob is the username they give).
I need that to check with the mysql database if that userid exists, then redirect it to the www.domain.com/affiliate/bob url.
Right now, I have this in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)$ affiliate/home.html?affiliate=$1 [L]
It obviously doesnt work and Ive never dealt with .htaccess before. Any help is greatly appreciated. Thank you.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^affiliate/([^/]+)/?$ /affiliate/home.html?affiliate=$1 [L]
As for redirecting to the nicer looking URL if someone enters the one with the query string:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /affiliate/home.html\?affiliate=([^\ &]+)&?([^\ ]*)
RewriteRule ^ /affiliate/%2?%3 [L,R=301]
There's no way to check if a username is in your database before the rewrite happens, using an htaccess file. There's ways to talk to a database in apache 2.4 or using a RewriteMap (which can only be defined in server or vhost config, not in htaccess) but you're better off doing the check in your home.html and just return a 404 if the user isn't found.
See my answer on an other similar question.
You want www.domain.com/affiliate/home.html?userid=bob to be redirected to /affliate/bob. You want /affliate/bob to be rewritten to /affiliate/home.html?userid=bob.
Add this to your .htaccess in your /www/ or /public_html/.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^affiliate=([^&]*)$
RewriteRule ^affiliate/home\.html /affiliate/%1? [R,L]
RewriteRule ^affiliate/(.*)$ affiliate/home.html?affiliate=$1&r=0 [L]
The first rule matches if the user tries to access affiliate/home.html?userid=bob. The RewriteCondition matches the part of the query string (after the ?) that you need to write the fancy url. %1 matches the first 'capturing group' in a rewrite condition. The ? at the end will clear the query string. [R] means it is a redirect (see the linked answer what that does). [L] means it will stop matching if this one matches.
The second rule is an internal rewrite. $1 matches the first 'capturing group' in the RewriteRule. The trailing &r=0 is there to stop the first rule from matching. It requires a RewriteCond %{REQUEST_FILENAME} !-f to function properly, because the fancy url is in the same directory is the file that handles it.
I need to change my .htaccess and there are two lines which I don't understand.
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
When I should use these lines ?
Not the place to give a complete tutorial, but here it is in short;
RewriteCond basically means "execute the next RewriteRule only if this is true". The !-l path is the condition that the request is not for a link (! means not, -l means link)
The RewriteRule basically means that if the request is done that matches ^(.+)$ (matches any URL except the server root), it will be rewritten as index.php?url=$1 which means a request for ollewill be rewritten as index.php?url=olle).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (olle?p=1 will be rewritten as index.php?url=olle&p=1.
L means if the rule matches, don't process any more RewriteRules below this one.
For more complete info on this, follow the links above. The rewrite support can be a bit hard to grasp, but there are quite a few examples on stackoverflow to learn from.
If the following conditions are true, then rewrite the URL:
If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
then rewrite the URL in the following way:
Take the whole request filename and provide it as the value of a "url" query parameter to index.php. Append any query string from the original URL as further query parameters (QSA), and stop processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Apache docs #flag_qsa
Another Example:
RewriteRule "/pages/(.+)" "/page.php?page=$1" [QSA]
With the [QSA] flag, a request for
/pages/123?one=two
will be mapped to
/page.php?page=123&one=two
This will capture requests for files like version,
release, and README.md, etc. which should be
treated either as endpoints, if defined (as in the
case of /release), or as "not found."
When a a rewrite rule to allow us to make friendly URL's with an ID number. The story is only pulled through the ID number, so the text at the end doesn't really matter.
RewriteRule ^news/([0-9]+)$ /news/$1/ [R=301,L]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1
Our problem comes when any file linked within /news/images/ , it gets redirected as well. So anything that displays an image from /news/images/ doesn't work.
Can someone help me out? How can we limit the rewrite so that it says "If it's in the /images/ subdirectory, don't rewrite the path"?
You could take the simple route and just avoid the rewrite if the file exists:
RewriteRule ^news/([0-9]+)$ /news/$1/ [R=301,L]
# Ignore the RewriteRule if the request points to a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !=tags [NC]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1
Alternatively, if you wanted to do the directory checking in case the resource request didn't point to a real file, but should directly generate a 404 response, you can try the following:
RewriteRule ^news/([0-9]+)$ /news/$1/ [R=301,L]
# Check if the path segment after /news/ corresponds to an existing directory
# and if so, don't perform the rewrite
RewriteRule %{DOCUMENT_ROOT}/news/$1/ !-d
RewriteCond $1 !=tags [NC]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1
I’m trying to use the following .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^images/
RewriteRule (.*) view.php?picid=$1 [L]
RewriteRule ^/user/(.*)$ /users.php?user=$1
I want two things to happen: Whenever someone requests /1234, it redirects to /view.php?picid=1234, and also when someone visits /users/bob, it redirects to /users.php?user=bob.
My code however, doesn’t seem to be working correctly.
There are several ways to do that. Here’s one that should work:
RewriteRule ^user/(.+)$ users.php?user=$1 [L]
RewriteRule ^([0-9]+)$ view.php?picid=$1 [L]
The first rule will catch any request that’s URI path begins with /user/ followed by one or more arbitrary characters. And the second will catch any request that’s URI path begins with / followed by one or more digits.
The initial problem with your rules is that the RewriteRule with (.*) will match everything.
If you do not want it to match a URL with a slash in it (such as users/bob), try ^([^/]*)$
Secondly, after a URL is rewritten, the new URL goes through your rules again. If you want to avoid matching something that has already been rewritten once, you should add a condition like
RewriteCond %{REQUEST_URI} !\.php