how would I rewrite in .htaccess:
url.com/NAME to url.com/view.php?=NAME
I've tried a couple of variations but I think i might be putting a slash in the wrong place. Any suggestions?
RewriteEngine on
RewriteRule ^(\w+)$ /view.php?somevar=$1 [L]
You have to set NAME as the value of some variable.
This example allows NAME to be any word.. if you want something specific... just replace \w+ with whatever specific thing you want.
Related
I have a problem concerning RewriteRules.
I'd like to move one page permanently, so I want to use a 301 redirect. I tried this:
RewriteRule ^page1/([A-Z].*)$ http://www.abs.nl/page1/vraag-$1 [R=301]
However this does not work. Can someone please tell me how I could fix this? I already tried for hours to find an answer.
Should I also use a %{HTTP_HOST} condition? I see this a lot but I don't know how it works.
A couple things:
Make sure that you have RewriteEngine On before your rewrite rule.
Your current regex is looking for a capital letter followed by any character, is this what you want?
Try this:
RewriteEngine On
RewriteRule ^page/(.*)$ http://www.abs.nl/page1/vraag-$1 [R=301,L]
That redirect will match on any string of characters following page/ in a URL.
I have below url(s)
www.localhost.com/profile.php?username=first.last
i would like to permanently redirect above url to using .htaaccess file. (apache server)
www.localhost.com/first.last
please also consider there are few other urls there but i dont want to touch them..like
www.localhost.com/message.php?id=12
www.localhost.com/editprofile.php?editname=first.last
www.localhost.com/uploadphoto.php?username=first.last
can anyone please help me.
thank you in advance.
You could try to handle the Query String with RewriteCond and pass the captured match to RewriteRule. You must exclude any .phpscripts of your rewriting rule otherwise it will create some problems with others URLs.
Don't forget to add the [QSA] tag after your RewriteRule otherwise it will not add the Query String parameters.
Maybe doing something like this:
RewriteEngine on
#serve any existing php scripts as usual
RewriteRule ^([a-zA-Z0-9]+\.php) - [L]
#and now handle your specific stuff:
RewriteCond %{QUERY_STRING} ^([a-zA-Z0-9]+\./[a-zA-Z0-9]+)$
RewriteRule ^(%1)$ /profile.php?username=%1 [QSA]
I don't test it but it should be a good beginning. You can read some good stuff here and inside the docs for mod_rewrite httpd 2.2 about how to write and handle specific rewriting use cases.
I've got this type of urls on my site (as you can see dashes usage is quite random):
http://www.example.com/my_sub_directory/this--is--the-page-title---excellent-title
http://www.example.com/my_sub_directory/this-is--the--page-title--excellent-title
http://www.example.com/my_sub_directory/this-is-the-page----title---excellent---title
And I would like to rewrite them in the following format
http://www.example.com/my-sub-directory/this-is-the-page-title-excellent-title
As you can see the numbers of dashes in the original url is variable. Is this possible? Can I do it for all urls or can I just do it globally?
Can you please also provide a simple example on how to rewrite this
http://www.example.com/my_sub_directory/
into this
http://www.example.com/my-sub-directory/
Many thanks
Is this possible? Can I do it for all urls or can I just do it globally?
Yes, but not with an .htaccess rule. You could use a RewriteMap prg: if you have root access to the system or vhost config. However what you can do is some think like this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?--.*)$ remapper.php?url=$1 [L]
Then use the remapper script to munge the url parameter and issue a header("Location: ...") to do a 302 redirect to the "normalised" URI.
Re: the my_sub_directory/ rewrite, this is trivial for a fixed directory string, but the general case would need to be handled as above:
RewriteRule my_sub_directory/ my-sub-directory/ [L]
I am caching pages in my (Rails) application based on subdomain. The pages for certain actions are cached to /public/cache/(subdomain)/. The application is running under Apache with Phusion Passenger. The caching is working fine. The problem is that Apache is not picking up the cached pages and bypassing Rails like it should be. My rewrite rules are wrong and I need help fixing them.
I have used, as one example of many, the suggestion located at: https://github.com/yeah/page_cache_fu#readme, which is as follows:
RewriteMap uri_escape int:escape
<Directory /var/www/example.com/current/public>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} GET [NC]
RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}%{REQUEST_URI}%{QUERY_STRING}.html -f
RewriteRule ^([^.]+)$ cache/%{HTTP_HOST}/$1${uri_escape:%{QUERY_STRING}}.html [L]
RewriteCond %{REQUEST_METHOD} GET [NC]
RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/index.html -f
RewriteRule ^$ cache/%{HTTP_HOST}/index.html
The problem with this is it seems to be expecting the directory to be the full http host (i.e. it's looking in cache/subdomain.example.com rather than just cache/subdomain).
Edit: Even when I change the Rails app to cache to cache/subdomain.example.com Apache still does not use them so it seems that there is more wrong than just the subdomain aspect.
Could someone please help me come up with the correct rule?
Edit(2):
I have simplified my rewrite to the following (just to try to get to a working starting point):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteCond ^stats$ cache/%1/stats.html [L]
I would think this would cause http://abc.example.com/stats to be rewritten to http://abc.example.com/cache/abc/stats.html
It is not. I also added a RewriteLog entry and what I see there makes me think it is trying to redirect to http://abc.example.com/var/www/example.com/current/public/cache/abc/stats.html. This is further confirmed by that if I add an 'R' option along with the 'L' I see in my browser http://abc.example.com/var/www/....etc. I.e. it seems to be appending the full document root instead of just the public facing part.
Of course the result of the above is that I get a 404 error returned to the browser.
Can you see what is still wrong with my rule?
Edit: It's actually a bug.
http://code.google.com/p/phusion-passenger/issues/detail?id=563
Alright, this looks like it should work, but it doesn't. I've done a lot of testing with this, and it seems like the problem is the ^([^.]+)$ in the RewriteRule. Now, I did Google this, and it seems like it's a common enough pattern, so I don't understand what the issue could be. I just know that when I use that pattern in a RewriteRule, the rule fails. If I change it to ^([^.]+), it seems to work.
Hopefully someone with more experience with mod_rewrite can come along and explain to us what the problem with that pattern might be.
Edit: I just realized the problem with ^([^.]+)$:
Since you're building a cache, then the "normal" file will exist in its usual place. The implication of this is that if you ask the server for /file then, depending on your configuration, it will say "hey, file doesn't exist, so let's try the default extension of .html!" and so it goes off and finds file.html. Now when you get to the RewriteRule, the ^([^.]+)$ regex will be matched against file.html NOT file.
The ^([^.]+)$ says "the start of the string, followed by as many non-period characters as you can grab, followed by the end of the string" which works fine against file because it contains no periods. It fails against file.html because ^[^.]+ will match against file, but where the regex then expects to find the end of the string (i.e. $), it instead finds .html and fails.
The reason ^(.*)$ works is that it's guaranteed that only .* will be the whole of the string, since .* matches "as many of any character" so there is no character that can possibly exist between the (.*) and $ portions of the regex. That's not the case with [^.]+.
In order to extract the subdomain, you're going to need to backreference a RewriteCond. Basically, if you capture a reference (i.e. encapsulate something inside parens) in a RewriteCond, those references are available to a RewriteRule which immediately follows it.
For example, if I wrote this:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com
Then the parentheses would capture the subdomain - note the () around [^.]+
If I were then to write a RewriteRule on the next line, the text captured above would become accessible as %1.
So your RewriteRule would look like this:
RewriteRule ^([^.]+) cache/%1/$1${uri_escape:%{QUERY_STRING}}.html [L]
Hope that helps.
I need a rewrite rule that will do an internal redirect from:
<domain>/directory/<anything>/<anything>.php to:
<domain>/directory/<anything>.php with <anything> passed as a parameter, and it needs to leave all other parameters alone.
I get headaches from mod_rewrite. My issue is <anything>.php, I cannot find any examples for anything like that.
Example: domain.com/directory/something/lol.php?param=value ought to internally redirect to domain.com/directory/lol.php?param=value&someotherparam=something
Thanks for the help! I have read through many tutorial sites, but I am in the dark here...
EDIT: Code tags added since it wasnt showing up properly >.>
Not all pages will have other parameters passed, some might have only one, some might have many. This is another bit that confuses me...
RewriteEngine On
RewriteBase /
RewriteRule ^directory/([^/]+)/([^/]+)\.php$ directory/$2.php?someotherparam=$1 [QSA,L]
This will redirect domain.com/directory/something/lol.php?param=value to domain.com/directory/lol.php?param=value&someotherparam=something, as requested. (I'll note that currently, none of the other replies do so.)
RewriteEngine On
RewriteBase /
RewriteRule ^rewrite/(.+)/lol.php$ rewrite/lol.php?someotherparam=$1 [QSA,R,L]