I have that rule in the .htaccess.
RewriteRule ^(.+)\.([0-9a-zA-Z]+)$ - [L,NC]
I don"t understand what is the "-" (the minus sign) for, just begin the [L,NC]
$ - [L,NC]
From the Apache mod_rewrite docs:
(dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
Effectively it means to take no action when that input URL pattern is matched. Following that with [L] makes sure no subsequent matches will be performed so the input URL is used "as-is". This can be used to exempt one specific pattern from being rewritten when it would otherwise be matched by a more general pattern.
You won't see rules like the one in question too frequently, because it is usually possible to achieve the same result by reordering the RewriteRule, or by modifying the more general matching pattern so it doesn't match the exempt one to begin with.
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
(http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule)
In combination with the L flag this indicates that these URLs should be processed without transformation and no other rules should be applied.
Related
I want to change URL format from :
https://example.com/modules/news/article.php?storyid=224039
to :
https://example.com/news/224039
Any one can help to write true .htaccess codes?
thanks
Untested:
RewriteEngine On
RewriteBase /
RewriteRule ^news/([0-9]+) /modules/news/article.php?storyid=$1 [NC,L]
The NC flag is for No Case, if you want case insensitivity. If not, remove this flag. The L is the Last flag, meaning it would be the last rule parsed in the given rewrite instance so further rewrites aren't used. This is a bit counterintuitive in the sense that Apache will re-read all the rules all over again from the beginning anyways after the rewrite to make sure it doesn't have to rewrite again, and is a gotcha for many people regarding infinite rewrite loops... Probably can also omit the L flag altogether, but is more expressive.
The RewriteEngine On can be omitted in Apache configurations that enable this in the httpd.conf file. It is best practice to put it on again before assuming the engine is on. The rewrite base / probably can be omitted, depends on how you write your RewriteRule. Finally the RewriteRule uses a regular expression on the left, the parenthesis stores the match, the brackets define a character list, 0-9 is the valid characters, could also use \d instead, the + means match 1 or more times. The expression on the right is what to replace it with. The leading slash can probably be omitted. Also note that due to the presence of a querystring on the right side, if a querystring was present on the left side, it will be discarded. If you want to merge query strings, use the QSA flag meaning querystring append, and then it will merge querystrings when adding your storyid. Finally the $1 means use the first match that was captured with parenthesis on the left.
Currently I have my htaccess configured so that when I type anything after the domain, it is treated as a get
RewriteRule ^(.*)$ /index.php?id=$1
so
www.example.com/test
will redirect to
www.example.com/index.php?id=test
now what I would like is the page to detect if after the first / there is the # character and do something diffirent
for example,
www.example.com/#test
goes to
www.example.com/index.php?abc=test
whilst still retaining the first rule, can this be done?
And as a bonus, if you know how to use the # symbol instead of the #, please do let me know, I tried putting NE flag in my rule but I had no luck.
I need to say - this directory and anything after do this.
/thedirectory/* How would I write this?
Also I'm trying to figure out what exactly /|$ means. And what !^ means.
RewriteCond %{REQUEST_URI} !^/thepage(/|$)
Can someone help me with this? Researching trying to find the answer to my questions isn't coming up with answers.
This is about RewriteCond and regular expressions
To answer !^ first, it is two things ! (RewriteCond)
CondPattern is usually a perl compatible regular expression, but there is additional syntax available to perform other useful tests against the Teststring:
1. You can prefix the pattern string with a '!' character (exclamation mark) to negate the result of the condition, no matter what kind of CondPattern is used.
and ^ (regex)
Regex vocabulary
^ Called an anchor, matches the beginning of the string
/|$ is also a regular expression
/ matches a slash and has no special meaning
| is a special symbol and means or
$ is also an anchor and means end of string
So /|$ translates to: match a slash or match end of string.
Well I would suggest understanding what you are using before using it. The condition you have is opposite of what you are wanting. You should be researching regex characters rather than basic .htaccess help because then that will tell you what those mean.
The ! means not as in if not this page, then. So you need to remove that since you want to match on that page. ^ means start of the line basically. $ means the end and nothing else after that. Don't really need that in this case.
What you probably want is simply.
RewriteCond %{REQUEST_URI} ^/thediretory/? [NC]
I could not find the exactly the same question on SO. I hope someone can help me out with this.
Say, user entered http://www.example.com/abc#!def, and what I want to do is remove all symbols in the ${REQUEST_URI} portion, then do a redirect to http://www.example.com/abcdef. The problem is that these symbols can occur anywhere in the string, e.g. #ab!cdeg and abcdef#! should both redirect to abcdef.
If I'm correct, there is no string replace function for mod_rewrite, so this seems impossible to do, but am I correct?
You can capture specific parts of an URL with regular expressions in a RewriteCond
or RewriteRule, but not remove arbitrary characters.
Furthermore, you will never see the hash character '#' and everything after it in a URL, because it is used by the client to navigate to a specific part of the document.
Update using the next flag:
RewriteRule (.*)[^a-zA-Z](.*) $1$2 [N]
This rule removes all characters, which are not ^ alphabetic.
I'm creating a rewrite rule which should include a parameter which could contain the character %, however when I add it to my rule it breaks my website and every page returns an error:
RewriteRule ^sale/([a-zA-Z0-9_-%]+)$ browse.php?id=$1
I wanted the parameter to be able to include characters, digits 0 to 9 and special characters -, _ and %.
If I remove the % it works fine but obviously I want that to be accepted as a character for example url :
http://www.websitename.com/sale/test%20parameter
Apache translates percentage-encoded character inside a url before feeding it to mod_rewrite. So if you want to accept %20 in your urls you need to just add a space inside your RewriteRule. Note however that space is also to separate the regulare expression and the replacement string in RewriteRule, so in the case of a space you need to escape it using \
You can use the B flag http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_b