I have some rewrite rules in an htaccess file. I'm still getting into it so theres a few things I'm unsure of.
Basically I want all pages (except the /register page) to be rewritten like this: http://www.example.com/about -> http://www.example.com?page=about
To get that right I wrote this rule:
RewriteRule ^([a-z-_1-9]+)+/?$ ./?page=$1&%{QUERY_STRING} [L]
I then wrote this rule below the one above thinking it would override it, but it doesnt...
RewriteRule ^register/?$ ./?page=login&option=register
So going to /register gives me a 404. However if I comment out the first rule then the register page works.
I was thinking it would work like CSS where writing a new rule below would take precedence.
How would I get this right and how do you override previously written rewrite rules?
Thanks!
.htaccess is not CSS -- especially when it comes to mod_rewrite instructions/rules.
The rules are executed from top to bottom. Therefore -- put more specific rules at top and then more generic at bottom.
In your case:
RewriteRule ^register/?$ ./?page=login&option=register [L]
RewriteRule ^([a-z-_1-9]+)+/?$ ./?page=$1&%{QUERY_STRING} [L]
Related
I have the following in my htaccess:
RewriteRule ^news/[a-zA-Z0-9-_]+?/(.+)$ /news [R=301,L]
This works how I want it to, for example if I go to /news/some-category/some-post it just redirects to /news which is great. However it is also affecting my uploads folder. Take the following URL for example:
/news/wp-content/uploads/2014/07/Emily-286x300.jpg
This gets redirected to /news as well so all my images are broken. Is there a way to tweak this rule so that it doesn't affect the wp-content/uploads directory?
If /news/some-category/some-post represent a 3-level subdirectories, you can use this rule
RewriteRule ^news/[^/]+/[^/]+/?$ /news [R=301,L]
Also, please note you'll have to clear your browser's cache before trying again such link /news/wp-content/uploads/2014/07/Emily-286x300.jpg (my rule will not match it but your last rule does and it's in browser's cache)
We have a website where we show clients creative work we have produced for them. We upload raw assets to a path like this:
x.com/clients/clientName/campaignName/size/
I have a PHP script which adds our branding, contact information and other information and pulls in the raw creative (usually a swf object). It is in this directory x.com/clients/index.php and it accepts a query string parameter ?path so it knows where to look for the creative.
I am trying to do an apache rewrite in .htaccess so that our designers can upload directly to the known folder structure but so that when you go to x.com/clients/clientName/campaignName/size/ it should rewrite to x.com/clients/index.php?path=clientName/campaignName/size/
I am currently using the following rewrite rule, which works for the first folder level e.g. x.com/clients/clientName/ does successfully rewrite, but any subsequent folders do not.
RewriteRule ^clients/([^/\.]+)/?$ /clients/index.php?path=$1 [L]
My RegEx's are terrible, so I'm stuck on what to do. Any help appreciated, thank you kindly.
Your regex is only matching urls like clients/xxxxxx/ because your pattern [^/\.]+ means one or many characters except "/" or "."
With your rule, it can't work for other subdirectories.
You can change your rule by this one
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
To avoid internal server error (code 500 which means an infinite loop in this case), you can do it this way
RewriteRule ^clients/index\.php$ - [L]
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
Is there a special reason you want to use regex? In my opinion you can just catch everything coming after /clients:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(.*/)?index\.php$ [NC]
RewriteRule ^clients/(.*)$ /clients/index.php?path=$1 [L]
The second line is to prevents redirect loops, because the index.php is also in the folder /clients and this would cause never ending redirects.
I am new to URL rewriting and getting a bit frustrated. I'm using Helicon on the server and have gotten most URLs to re-write correctly.
(I had to remove the '//'s to allow me to submit the questions, but the urls are the standard http:// version)
My last task is to get these:
http://example.com/Object/?page=1
http://example.com/Object/?page=1&pagesize=10
http://example.com/Object/?page=1&pagesize=10&backcolor=red
to
http://example.com/default.aspx?resource=Object&page=1
http://example.com/default.aspx?resource=Object&page=1&pagesize=10
http://example.com/default.aspx?resource=Object&page=1&pagesize=10#backcolor=red
Preferably I'd like one rule to handle all 3 possibilities, but if I need to make 3 rules, one for each, and add a [L] or something at the end that would be ok too. I just can't get the querystring parsing right.
Here is an existing rule I have that works to give you an idea of what I've been doing:
RewriteRule ^/([a-zA-Z0-9]+)(/([a-zA-Z0-9]+)(/([a-zA-Z0-9]+))?)?/?($|\?) /default.aspx?resource=$1&id=$3&option=$5 [L]
It's for a separate example, but the syntax shows what I'm doing.
Does this work for you?
RewriteRule ^\/([^/]*)\/\?(.*)$ /default.aspx?resource=$1&$2 [L]
[Update]
Try this:
RewriteRule ^([^/]*(?=\/)|[^?]*(?=\?)|.*)($|[^\?]*\?(.*)) /default.aspx?resource=$1&$3 [L]
I've done RewriteRule for index page in my website.
Here is the code below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/?$ /index.php?origin=$1
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/([a-zA-Z0-9+\-]+)/?$ /index.php?origin=$1&gender=$2
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/([a-zA-Z0-9+\-]+)/([a-zA-Z0-9+\-]+)/?$ /index.php?origin=$1&gender=$2&type=$3
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/([a-zA-Z0-9+\-]+)/([a-zA-Z0-9+\-]+)/([a-zA-Z0-9+\-]+)/?$ /index.php?origin=$1&gender=$2&type=$3&page=$4
</IfModule>
and the url is looks like
http://babynames.agurchand.com/scottish/male/pythagorean
It's a single page website, so everything is working fine so far.
Then i wanted to add one more page on my website and i have added too. file name is 'namemeaning.php'.
I've added the below code in addition of the htaccess pasted, at the bottom. But It doesn't seem to be working.
RewriteRule ^/namemeaning/([a-zA-Z0-9+\-\(\)]+)/?$ /namemeaning/namemeaning.php?name=$1
I've tried with the below url, but I'm still being referred to the index page only.
http://babynames.agurchand.com/namemeaning/abasi
Can anyone give me a solution for this please!
You're being forwarded to the index page because your regular expression for the index page fits your request. You're being sent to index.php?origin=namemeaning&gender=abasi
http://babynames.agurchand.com/namemeaning/abasi
'namemeaning' fits the rule ([a-zA-Z0-9+\-\(\)]+)
'abasi' fits the rule ([a-zA-Z0-9+\-]+)
A simple fix would be adding the line
RewriteRule ^/namemeaning/([a-zA-Z0-9+\-\(\)]+)/?$ /namemeaning/namemeaning.php?name=$1
above the line
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/?$ /index.php?origin=$1
You should remember to always place the most specific RewriteRule(s) above those that are more general.
For more information about regular expressions, I can advice you this website.
The rewruite rules are checked sequentially from top to bottom, and all rules matched are applied in that order. Double check that none of your rules match before the desired one.
Good Tool for that purpose is http://www.regextester.com/
I've created the following .htaccess file after hours of work,
Everything seems to be working properly, however I'm new to mod_rewrite, and I think my code is amateurish, so I'm looking for things to improve.
For example I thought if I use [L] at the end of a rule, the rest of rewrites will be ignored, but looking at the rewrite logs I see that they are not, there are multiple unwanted pattern matchings that certainly will slow everything down.
Also I have a book that says [C] will chain rewrite conditions, but my apache throws
http://pastebin.com/62JyBXdS
The [L] flag does indeed prevent further rules from processing, however the rewritten url could be passed back through all of your rules a second time hence the multiple entries in your log - see the manual page http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_l
Alot of your rewrite rules do the same thing with just different data and could be compacted down to a single regex, I've done a few but you could do the entire list.
RewriteRule ^/([dprcmlfb]|members|lnli|freelisting)/(.*)$ /$1\.php/$2 [L]
if you also add a RewriteCond of somethine like
RewriteCond %{REQUEST_URI} !^/[^/]+\.php
to prevent the rule firing for a php file request
You could add the MultiViews option instead of rules like the rule below:
RewriteRule ^/d/(.*)$ /d\.php/$1 [L]
MultiViews would correctly interpret /d/stuff as a request to d.php if no other rule interferes.