I have a number of URIs that read as follows;
http://www.domain.com/index.php?route=information/information&information_id=2 (about)
http://www.domain.com/index.php?route=information/information&information_id=4 (help)
http://www.domain.com/index.php?route=information/information&information_id=7 (contact)
....
Is there a way I could avoid writing explicit but similar rewrite rules?
EXAMPLE
RewriteRule ^contact$ index.php?route=information/information&information_id=2 [NC,L] # About
RewriteRule ^help$ index.php?route=information/information&information_id=4 [NC,L] # Help
Lots of possibilities. Easiest might be something like this:
^(.*)$ index.php?route=information/information&information_id=$1
Then handle the information_id string in your index.php. Make sure it only allows acceptable values and 404s everything else.
Or to be more restrictive at the mod_rewrite level:
^(contact|help|foo)$ index.php?route=information/information&information_id=$1
If you have more than a handful of pages to rewrite, the key is a to have a single, simple rewrite rule combined with logic in your index page to serve the correct content based on the URL requested.
Take a look at the .htaccess and index.php for any popular CMS which supports url rewriting and you will find what you seek.
Related
below is my requirement :
Supposing i have a URL coming from browser
http://test.aroa.com/mance
I need to add /xyz/abc/xy/ before perfomance and .html after it i.e. using re-write rules of mod_rewrite to change it to below URL once it hits the dispatcher in AEM(or apache web-server)
http://test.aroa.com/xyz/abc/xy/mance.html
For this i wrote the below re-write rule along with some rewrite conditions(rewrite conditions not here)
RewriteRule ^(/.*)$ /xyz/abc/xy$1.html [P,L]
It works for the this site but messes up some other functionalities by adding /xyz/abc/xy to other URLs as well
Can you suggest me some way using which i can restrict the URL rewriting only for the //test.aroa.com/ URL and not affect any other URL
I tried putting the rule inside directory tag of with the doc-root name inside it. but it fails to get applied in that case..
Can anyone suggest something that can help
If you want to apply your rule on a specific domain, you can add this condition
RewriteCond %{HTTP_HOST} ^test\.aroa\.com$ [NC]
Also, a small semantic detail: you don't need L flag with P flag (it's redundant, since P includes L)
Finally, your code should look like this
RewriteCond %{HTTP_HOST} ^test\.aroa\.com$ [NC]
# your other conditions
RewriteRule ^/?(.+)$ /xyz/abc/xy/$1.html [P]
Remark: i think you don't need to use mod_proxy (P flag) especially because it's slower than a simple internal rewrite. If your (sub-)domains share the same document root then you can avoid using it by replacing P flag with L flag
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'm trying to use mod_rewrite to point the blog portion of a site to a blog site.
this is what I have to handle the normal stuff
RewriteRule ^(\w+)/?$ index.php?page=$1
This is what i'm trying to use for the blog site
RewriteRule ^blog/?$ http://url.to.my.blogger.site
but it's not working, when I go to site/blog it directs me to index.php?page=blog is there something I need to do to not do the second rewrite if the first is correct? like an if/else? sorry don't know much about mod_rewrite so any advice would be awesome.
also I noticed that if I try to do something like site/home everything works fine but if I attempt to hit site/home/ it puts all of my urls into the wrong context, for example my css and images don't get loaded correctly.
my full file is this
RewriteEngine on
RewriteRule ^blog/?$ remote/blog/uri/here
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php - [F]
and when i hit site/blog it still tries to serve index.php?page=blog, I'm guessing I have to break out of the code at some point? I couldn't find documentation on if/else statements
I needed to add flags to my RewriteRule lines so that the server wouldn't evaluate further. Changing them to be
RewriteRule ^/blog http://url.to.blog [L]
did the trick, the problem was that it was evaluating all the way down, seeing as I wasn't attempting to go to index the last valid rule to evaluate was the general rewrite rule.
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've been working on a solution to this for several hours now & figured if someone doesn't mind helping me out, it might save me some time. My question is with regards to Apache mod_rewrite; of course there is tons of documentation out there, however nothing specific to my requirements which are:
to take a URL in this format:
language/pagename.php
(language will either be 'english' or 'french', I will write a separate rule for each. [only need an example for one though]. page name will be any word character (w+). all URLs will have a .php extension).
And then rewrite it so the URL doesn't change in the users browser, but so that php could receive it in this format:
language/page.php?slug=pagename
e.g. so $_GET['slug'] would return the value pagename, and all requests are then handled by page.php.
So far my best guess is
RewriteEngine On
RewriteBase /
RewriteRule ^english/(\w+).php$ english/page.php?slug=$1
However this make php tell me that slug=page for this URL for example english/financial.php; rather than financial.
Have tried a bunch of other regex conventions too (.) instead of w & so on..
Use these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(english|french)/([^/\.]+)\.php$ /$1/page.php?slug=$2 [NC,QSA,L]
This needs to be place in .htaccess file in root folder. If you will be placing it config file (e.g. httpd-vhost.conf, inside <VirtualHost> directive), then rule needs to be slightly altered.
This rule should work for any language, as long as you add it into the rule (english|french part).
This rule has a condition which will not rewrite if such file already exists. This should solve your problem with slug=page: in your rule you most likely have a rewrite loop (after rewrite occurs it goes to the next iteration -- that's how mod_rewrite works, and you need to have some logic in place to break this loop). Instead of RewriteCond %{REQUEST_FILENAME} !-f you could use RewriteCond %{REQUEST_URI} !^/(english|french)/page\.php [NC] but it is a bit more difficult to maintain (you need to add languages here as well as in rewrite rule itself).
If you already have some other rewrite rules then take care with placing these in correct place (order of rules matters).
Because I do not know for sure what page names (slugs) would be, I've used this pattern: [^/\.]+ (any characters except / or .) .. but you may change it to \w+ or whatever you think will be better.
Rule preserve any optional page parameters (query string) -- useful for preserving referrals/tracking code etc.