I'm trying to get the following path: /faculty/index.php?PID=FirstLast&type=alpha
To rewrite to this: /faculty/FirstLast
Am I correct to assume the following would be acceptable to put in .htaccess?
# Rewrite old URLS
RewriteCond %{QUERY_STRING} ^PID=([0-9a-zA-Z]*)$
RewriteRule ^/faculty/index.php$ /faculty/%1 [R=302,L]
I'm okay to throw away any other query string variables. I'm applying these rules at the .htaccess file level. This project is a migration from an older system into Drupal.
Outcome:
My .htaccess looks like
# Rewrite old URLS
RewriteCond %{QUERY_STRING} PID=([0-9a-zA-Z]*)
RewriteRule ^faculty/ /faculty/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} vidID=([0-9]*)
RewriteRule ^videos/ /video/id/%1/? [R=301,L]
I also found this wonderful tool -- a mod_rewrite tester
http://htaccess.madewithlove.be/
All good!
Try this instead:
RewriteRule ^faculty/index.php$ /faculty/%1? [R=302,L]
The leading slash is not in the URI-path tested in the rule, so can't be in the regex either.
As the query is automatically appended to the substitution URL (passed through unchanged) unless a new query is created in the rule, the trailing question mark ? erases the existing query string when the rule is used.
Related
Using ISAPI REWRITE v3
I need to replace a string in an URL. The old string is 'p=type1_' and the new is 'p=type2_'. So
http://www.somesite.com/cgi-bin/script.pl?t=something&p=type1_abcde
becomes
http://www.somesite.com/cgi-bin/script.pl?t=something&p=type2_abcde
I reckon this should work:
RewriteRule ^(.*)p=type1_(.*)$ $1p=type2_$2 [NC]
But I get a 'Pattern Not Matched' in the ISAPI_REWRITE RegexTest app as soon as the original string contains a '?' - which, in practice, it always would.
How do I do this simple search and replace?
You will need to use RewriteCond %{QUERY_STRING} to search the querystring.
Try something like this:
RewriteCond %{QUERY_STRING} t=(.*)&p=type1_(.*)
RewriteRule ^cgi-bin/script.pl$ cgi-bin/script.pl?t=$1&p=type2_$2 [NC]
I am trying to use mod_rewrite RewriteRule, and in my RewriteRule, I am trying to match a URL that has query string that looks like:
http:///myfakeoam/obrareq.cgi?....
My RewriteRule looks like:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^wh=(.*)$ [NC]
RewriteRule ^/myfakeoam/obrareq.cgi$ http://apache1.whatever.com/formbasicprotected/index.html [CO=wh:%1:.whatever.com:1440:/]
When I test manually, by manually typing the URL into the browser, that RewriteRule seems to be able match a request if the request looks like:
http:///myfakeoam/obrareq.cgi?wh=xxx&ru=yyyy&....
but, in my actual system, the request is being created by anoher app, and it appears to be URL-encoding (actually URL-encoding twice) the query string (e.g, replacing equal with "%3D", etc.), i.e.:
http:///myfakeoam/obrareq.cgi?wh%3Dxxx....
And if the query string part is URL-encoded like that the pattern match in my RewriteRule is not failing...
Is there any way to handle this situation?
Thanks,
Jim
Answering my own question, I just realized that the RewriteCond was just doing a regex match for the query string, so I changed that to:
RewriteCond %{QUERY_STRING} ^wh%3D(.*)$ [NC]
and then it worked.
Jim
I have browsed the other topics, including this one: Mod_rewrite invisibly: works when target is a file, not when it's a directory, but I can't find a solution to my problem.
I have the following rewriting rule:
RewriteRule ^([a-zA-Z0-9_-]+)$ ./index.php?s=$1 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ ./index.php?s=$1 [L,NC]
What it does is to write anything like http://myaddress/parameter to http://myaddress/index.php?s=parameter and show this new rewritten address in the browser's address bar.
How can I make rewriting without showing the rewritten URL in the address bar?
Edit
This is the content of my .htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^([a-z0-9_\-]+)/?$ index.php?s=$1 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?$ index.php?u=$1&s=$2 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?$ index.php?u1=$1&u2=$2&s=$3 [L,NC]
1. No need for 2 rules that do the same job (the only difference is presence of trailing slash).
2. No need to have a-zA-Z in pattern if you have [NC] flag -- a-z is enough.
3. Try rule without ./
Considering all the above mentioned the rule will become:
RewriteRule ^([a-z0-9_\-]+)/?$ index.php?s=$1 [L,NC,QSA]
P.S.
I have also added the QSA flag to preserve original query string (if present).
The rule is tested and is working fine. If it still does not work for you then post ALL rewrite rules that you have.
This should work:
RewriteEngine On
RewriteRule ^([-a-zA-Z0-9_]+)$ index.php?s=$1 [L]
RewriteRule ^/?$ index.php [L]
is possible to exclude a url being parsed by mod rewrite?
my .htaccess has rewrite rules like
RewriteRule ^contact contact_us.php
and a couple more static pages.
currently my site don't have troubles cause uses http://domain.com/user.php?user=username
but now i need rewrite to:
http://domain.com/username
I've tried with:
RewriteRule ^(.*)$ user.php?user=$1 [L]
but all my site stops working...
is possible to avoid parse my static pages like contact/feed/etc being treated like usernames?
edit to match david req:
this is my actual .htaccess file:
RewriteEngine On
Options +Followsymlinks
RewriteRule ^contact contact_us.php [L]
RewriteRule ^terms terms_of_use.php [L]
RewriteRule ^register register.php [L]
RewriteRule ^login login.php [L]
RewriteRule ^logout logout.php [L]
RewriteRule ^posts/(.*)/(.*) viewupdates.php?username=$1&page=$2
RewriteRule ^post(.*)/([0-9]*)$ viewupdate.php?title=$1&id=$2
RewriteRule ^(.*)$ profile.php?username=$1 [L]
also i've enabled modrewrite log my first file:http://pastie.org/1044881
Put the rewrite rules for the static pages first, and add the [L] flag to them:
RewriteRule ^contact contact_us.php [L]
...
then after those, use your rewrite rule for the username:
RewriteRule ^(.*)$ user.php?user=$1 [L]
(hopefully nobody has a username of contact).
EDIT: Based on the log output you posted (which I'm assuming corresponds to an unsuccessful attempt to access the contact page... right?), try changing the contact rewrite rule to either
RewriteRule ^contact$ contact_us.php [L]
or
RewriteRule ^contact contact_us.php [L,NS]
That is, either add $ to make the pattern match only the literal URL contact, or add the NS flag to keep it from applying to subrequests. According to the log output, what seems to have happened is that Apache rewrites contact to contact_us.php and then does an internal subrequest for that new URL. So far so good. The weird thing is that the ^contact pattern again matches contact_us.php, "transforming" it to contact_us.php, i.e. the same thing, which Apache interprets as a signal that it should ignore the rule entirely. Now, I would think Apache would have the sense to ignore the rule only on the subrequest, but I'm not sure if it's ignoring the entire rewriting process and leaving the original URL, /contact, as is. If that's the case, making one of the changes I suggested should fix it.
EDIT 2: your rewrite log excerpt reminded me of something: I'd suggest making the rewrite rule
RewriteRule ^([^/]+)$ user.php?user=$1 [L]
since slashes shouldn't be occurring in any usernames. (Right?) Or you could do
RewriteRule ^(\w+)$ user.php?user=$1 [L]
if usernames can only include word characters (letters, numbers, and underscore). Basically, make a regular expression that matches only any sequence of characters that could be a valid username, but doesn't match URLs of images or CSS/JS files.
The -f and -d options to RewriteCond check if the current match is a file or directory on disk.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ....
Im trying to forward this url, but its not working - here is my code:
RewriteRule ^https://www.wsjwine.com/discovery_offer.aspx?promo=2227006$ https://www.wsjwine.com/discovery_offer_lp2.aspx?promo=2227006 [L]
With RewriteRule directive you can only test the URL path. For further tests you need to use additional RewriteCond directives.
Now if you want to rewrite every request of /discovery_offer.aspx to /discovery_offer_lp2.aspx regardless of how the query looks like, you can just use this (example for the .htaccess file in the root directory):
RewriteRule ^discovery_offer\.aspx$ discovery_offer_lp2.aspx [L]
If you don’t specify a query in the substitution, the originally requested query is automatically appended to the new one.
And if you just want to rewrite that specific URL, try this:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} =www.wsjwine.com
RewriteCond %{QUERY_STRING} =promo=2227006
RewriteRule ^discovery_offer\.aspx$ discovery_offer_lp2.aspx [L]
You can't detect query strings like that. Use RewriteCond %{QUERY_STRING}.