mod rewrite - multiple variables - mod-rewrite

Apologies if this is a basic solution but mod rewrite still give me gip.
I have a current mod rewrite rule to convert a slug into a usable variable like so:
RewriteRule ^/c/([a-z0-9_\-]+)/?.*$ /content.asp?slug=$1 [L]
I need to add a second, optional variable. Only the slug matters, the second variable may or may not be there.
www.domain.com/this-is-the-slug/
and
www.domain.com/this-is-the-slug/optional-variable/
should both work. I tried:
RewriteRule ^/c/([a-z0-9_\-]+)/([a-z0-9_\-]+)/?.*$ /content.asp?slug=$1&scotland=$2 [L]
But now every page (without the optional variable) just redirects to the root.
any ideas?

I worked it out.
I need both rules:
RewriteRule ^/c/([a-z0-9_\-]+)/([a-z0-9_\-]+)/?.*$ /content.asp?slug=$1&scotland=$2 [L]
RewriteRule ^/c/([a-z0-9_\-]+)/?.*$ /content.asp?slug=$1 [L]

Related

mod_rewrite: transform = and & in slashes

I was just looking for a solution to transform any =,?,& found in a query string into a simple slash /.
To be more specific, my link is something like:
http://www.mydomain.com/product.php?c=1&sc=12&products_id=15
and I would it like this:
http://www.mydomain.com/product.php/c/1/sc/12/products_id/15
whatever the master page could be (in this case is product.php, but it could be foo.php, bar.php...or else).
I have googled a lot but didn't find any good solution to achieve what i'm looking for.
I have found complex rewrite rules, but they all include the "page name" into them:
i.e.
RewriteRule ^/?index/([^/]+)/([^/]+)$ /index.php?foo=$1&bar=$2 [L,QSA]
That rule is only applicable to index.php and to known variables like foo, bar.
I need a more general one, whatever the master page is, whatever the variables are.
Can this be done?
Any suggestion?
Thanks
I assume you're using apache >= 2.2. Add this to your apache conf:
<IfModule mod_rewrite.c>
RewriteEngine On
# you absolutely need to use RewriteBase if this snippet is in .htaccess
# if the .htaccess file is located in a subdirectory, use
# RewriteBase /path/to/subdir
RewriteBase /
RewriteCond %{QUERY_STRING} ^(=|&)*([^=&]+)(=|&)?(.*?)=*$
RewriteRule ^(.*)$ $1/%2?%4= [N,NE]
RewriteCond %{QUERY_STRING} ^=$
RewriteRule ^(.*)$ $1? [R,L]
</IfModule>
The first RewriteCond/RewriteRule pair repeatedly matches a token delimited by & or = and adds it to the path. The important flag is the [N] that causes the whole ruleset to start over again, as often as the rule matches. Additionally, a = is appended to the end of the query string. This is to create a mark in the URL that at least one rewrite has happened.
The second ruleset checks for the = mark that remains after the URL has been rewritten completely and issues a redirect.
Have a look at http://wiki.apache.org/httpd/RewriteQueryString for some useful hints.

Rewrite Query String with .htaccess

I'm trying to do a very simple rewrite of a query string
http://www.example.com/library.php?q=abscessed-tooth
to
http://www.example.com/library/abscessed-tooth
This is the code that I've written in my .htaccess file and it is doing nothing
RewriteEngine On
RewriteRule ^/library/?([^/]*)/?\/http://www.example.com/library.php?q=$1 [L]
Maybe likely .htaccess files are not considered in your environment. If in doubt turn on RewriteLogging as it is explained in the excellent documentation of the rewriting module.
Oh, and check the error log, you have a syntax error in the RewriteRule anyway: RewriteRule takes 2 arguments plus flags, your rule has only a single argument:
RewriteEngine On
RewriteRule ^library/([^/]*) http://www.example.com/library.php?q=$1 [L]
You need dollar sign in the end of "left" part not question mark:
^/library/([^/]*)/$ http://www.example.com/library.php?q=$1 [L]
Also do you need the question mark between / and ( ? It doesn't look like lookahead or lookbehind?
Try without wrapping slashes as well
^library/([^/]*)$ http://www.example.com/library.php?q=$1 [L]

.htaccess RewriteRule for change the path if a word appears in the path

:-)
I need a .htaccess RewriteRule that can do this:
If the browser load this url path:
http://www.domain.com/example/word_to_change/
change to
http://www.domain.com/example/new_word/
take a look that the "word_to_change" and "new_word" appears in the third level in the path
I was tried this:
RewriteRule ^(word_to_change/)?$ /new_word/ [R,L]
But only works if the "word_to_change" appears in the second level, not in the third.
thanks for your help! :)
ADDED:
three examples:
need to change
http://www.domain.com/second_level_with_any_word/specific_word_1 or
http://www.domain.com/example_1/specific_word_1 or
http://www.domain.com/example_2/specific_word_1
to
http://www.domain.com/second_level_anything/specific_word_2 or
http://www.domain.com/example_1/specific_word_2 or
http://www.domain.com/example_2/specific_word_2
maybe is mor simple for explain with this other example:
http://*/*/specific_word1
for
http://*/*/specific_word2
This depends on what to acomplish, if you want to change ANY word that apeaers after example with a SPECIFIC word then what you need is:
RewriteRule ^example/(.*) /new_path/ [R,L]
if you want to pass that word as a paramater you can use
RewriteRule ^example/(.*) /new_path/process.php?word=$1 [R,L]
if you want to send any URL that ends with a specific word try this
RewriteRule ^(.*)/old_word/$ /$1/new_word/ [R,L]

Re-write userprofile URL (remove.html)

Any Ideas how to change remove .html in this mod-rewrite script
Doesnt work if I remove ".html"
RewriteRule ^([^/]*)\.html$ /userprofile.php?member_id=$1 [L]
Works as
http://site.com/12.html
but wants to have it as
http://site.com/12
Thank you
To make the .html optional, put it in a group and use the ? quantifier:
RewriteRule ^([^/]*)(\.html)?$ /userprofile.php?member_id=$1 [L]
But as this pattern will now also match any single path segment, you should make it more specific to only match your specific URL path pattern. In this case \d+ instead of [^/]* would be a better choice:
RewriteRule ^(\d+)(\.html)?$ /userprofile.php?member_id=$1 [L]
MODIFIED:
I have tested the following and they work on a CentOS server running Apache.
For directory rewriting:
RewriteRule ^/test/(.*)$ http://www.google.com [L,R]
That will redirect http://www.site.com/test to http://www.google.com.
For all files in a directory this will work:
RewriteRule ^/test/([^/]*)(.*)$ http://www.google.com [L,R]
That will redirect www.site.com/test/12.html or www.site.com/test/298.aspx or any other file in the "test" directory to www.google.com.
So this may be more what you are looking for:
RewriteRule ^/12/([^/]*)(.*)$ /userprofile.php?member_id=$1 [L,R]
ORIGINAL POST:
I believe this is what you are looking for:
RewriteRule ^([^/]*)(.*)$ /userprofile.php?member_id=$1 [L]
That is if you are trying to do the rewrite for files...
It will be slightly different if you want to do the rewrite against a directory.
You need to remove the backslash as well ("\.html").

Having problems with a Rewrite rule

I have an application most of it is still in development that's why i need to block access on all pages but just not two of them.
Suppose i have a.php all requests will be redirected to here except b.php.
So i think there should be 3 rules:
1st: when a.php and b.php are requested they should be visible to user,
2nd: if anything other than those two is requested,
it should be redirected to a.php.
3rd: for external css,javascript and image files
there should be an access rule
Since i dont have much experience with server administration, i believe i'm totally lost:)
This is what i tried:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^/b.php
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ a.php
In practice you would swap the second and third rule as your second rule would be the default route:
# 1st
RewriteRule ^(a\.php|b\.php)$ - [L]
# 3rd
RewriteRule \.(js|ico|txt|gif|jpg|png|css)$ - [L]
# 2nd
RewriteRule !^a\.php$ a.php [L]
The subsitution - means that the URI is not changed.
I am not sure what you means about #3 but see from what you are trying, I guess you means, all js,ico,... are to be redirect to a.php. Is that right?
If so, that means, a => a.php (with parameter), b => b.php (with parameter) and other => a.php. So try this:
RewriteBase /
RewriteRule ^a\.php(.*) /a.php$1
RewriteRule ^b\.php(.*) /b.php$1
RewriteRule ^.* /a.php
But if you means that all those script and media files are to be accessible normally, try this:
RewriteBase /
RewriteRule ^a\.php(.*) /a.php$1
RewriteRule ^b\.php(.*) /b.php$1
RewriteRule ^(.*)\.(js|ico|txt|gif|jpg|png|css)$ /$1.$2
Basically, the rewrite rule is the regular expression and $1,$2,... are match-string group (those wrapped with "(" and ")").
Hope this help.

Resources