I'd like to use apaches mod_rewrite to rewrite my url's. Normally, that i could do that, but i think is a special case. The content on the page depends on the url parameters, but is also capable of altering those parameters, and thus gaining new content without refreshing. The same parameters would always give the same result, however.
My url currently looks like this:
http://example.com/#?div1=1&div2=5
I would like to be able to use a neat, clean url, like http://example.com/nameofarticle. Does using mod_rewrite prevent changing a parameter that is "hidden" beneath such a url? And if not, how can i use mod_rewrite in order to allow me to keep working like this?
Thanks in advance!
I think you need the RewriteCond %{QUERY_STRING} before your RewriteRule. Something like:
RewriteCond %{QUERY_STRING} div1=(.*)&
Then you can access the bracketed values in the RewriteRule with %1 ... Just as you'd use $1 for bracketed values in the RewriteRule itself.
You may also want to be looking at the RewriteRule flag [QSA]. Quoting the mod_rewrite docs:
'qsappend|QSA' (query string append)
This flag forces the rewrite engine to
append a query string part of the
substitution string to the existing
string, instead of replacing it. Use
this when you want to add more data to
the query string via a rewrite rule.
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 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.
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]
Okay its been a while since I used mod rewrite and I created a couple of mod rewrites along time ago and forgot what they did and I was wondering what exactly does this code snippet do. Can someone please be as detailed as possible as to what the snippet does. Thanks!
Here is my mod rewrite code.
RewriteRule ^/?sitemap.xml?$ sitemap.php [L,NC,QSA]
Basically anything that looks for sitemap.xml will be passed to sitemap.php but without it showing to the user, that is, the url doesn't change for the user. Here is some of the documentation:
Taken from the Apache mod_rewrite Flags:
RewriteRule pattern target [Flag1,Flag2,Flag3]
L|last
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
If you are using RewriteRule in either .htaccess files or in sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it. It is possible that as the rewritten request is handled, the .htaccess file or section may be encountered again, and thus the ruleset may be run again from the start. Most commonly this will happen if one of the rules causes a redirect - either internal or external - causing the request process to start over.
It is therefore important, if you are using RewriteRule directives in one of these context that you take explicit steps to avoid rules looping, and not count solely on the [L] flag to terminate execution of a series of rules, as shown below.
The example given here will rewrite any request to index.php, giving the original request as a query string argument to index.php, however, if the request is already for index.php, this rule will be skipped.
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*) index.php?req=$1 [L]
NC|nocase
Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.
In the example below, any request for an image file will be proxied to your dedicated image server. The match is case-insensitive, so that .jpg and .JPG files are both acceptable, for example.
RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
QSA|qsappend
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
Consider the following rule:
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. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.
When "sitemap.xm" is requested redirect the request to "sitemap.php" instead. "L" means leave (or skip any following rules)
NOTE
QSA flag has to do with Query String handling (combine the old and new). I couldn't find anything about NC.
The reference has the full detail (Apache).