Mod-rewrite shenanigans - mod-rewrite

I have this rewrite rule
RewriteEngine On
RewriteBase /location/
RewriteRule ^(.+)/?$ index.php?franchise=$1
Which is suppose to change this URL
http://example.com/location/kings-lynn
Into this one
http://example.com/location/index.php?franchise=kings-lynn
But instead I am getting this
http://example.com/location/index.php?franchise=index.php
Also, adding a railing slash breaks it. I get index.php page showing but none of the style sheets or javascript are loading.
I'm clearly doing something very wrong but I have no idea what despite spending all day R'ingTFM and many online primers and tutorials and questions on here.

Your problem is you are redirecting twice.
'location/index.php' matches the regex ^(.+)/?$
You want to possibly use the "if file does not exist" conditional to make it not try mapping a second time.
RewriteEngine on
RewriteBase /location/
RewriteCond %{REQUEST_FILENAME} !-f # ignore existing files
RewriteCond %{REQUEST_FILENAME} !-d # ignore existing directories
RewriteRule ^(.+)/?$ index.php?franchise=$1 [L,QSA]
And additonally, theres the [L,QSA] which tries to make it the "last" rule ( Note, this is not entirely obvious how it works ) and append the query string to the query, so that
location/foobar/?baz=quux ==> index.php?franchise=$1&baz=quux
( i think )

It sounds to me as though the rewrite filter is executing twice. Try adding a last flag
RewriteRule ^(.+)/?$ index.php?franchise=$1 [L]

Your rule self matches, and therefore it will reference itself.

Related

apache mod_rewrite from /a/b/c to /a.php?arg1=b&arg2=c

I am trying to do a rewrite. After reading the URL Rewriting Guide, I'm still a bit confused.
I want to take /blog/123 and make it become /blog.php?blog=123, but I cannot figure out why it's not working. I know that mod_rewrite is on. My .htaccess file is:
RewriteEngine ON
RewriteRule ^/blog/([^/]+)/?$ /blog.php?blog=$1
Any help would be greatly appreciated.
I found the solution to my issue. I was making it way too hard.
RewriteCond /blog/%{REQUEST_FILENAME} !-d
RewriteRule /blog/([0-9]+)$ /blog.php?blog=$1
The first thing I needed to to was set the condition. The %{REQUEST_FILENAME} !-d variable means to run the rule if the directory is non-existant. It then matches on a number to the end of the string.

Mod_Rewrite for URL

I have been looking through questions and answer for days trying to figure out how to make this work.
So far I can get my URL to change, but it won't load the page.
I have to take
http://www.mysite.com/index.php?mode=about
And have it show up as
http://www.mysite.com/about/
So far I have the following code:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^mode=(.*)
RewriteRule ^ http\:\/\/\www.mysite.com\/%1? [R=301,L]
RewriteRule /(.*) /index.php?mode=%1 [L]
I have changed things multiple times and nothing. Most site seem to tell me I don't need the 301 redirect but then I can't get anything to work.
For (your) example, once you've properly routed from mysite.com/index.php?mode=about to mysite.com/about, it's now going to look at mysite.com/about/ to find what comes next (index.py/index.html/etc).
Because there is nothing at /about/, you're getting a 404 error.
I don't think you can use mod_rewrite to do exactly what you're trying to achieve, without having some handling within /about/ to actually display the page you want once you get there.
http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html
Remember the Filesystem Always Takes Precedence
The filesystem on your server will always take precedence over the
rewritten URL. For example, if you have a directory named “services”
and within that directory is a file called “design.html”, you can’t
have the URL redirect to “http://domain.com/services”. What happens is
that Apache goes into the “services” directory and doesn’t see the
rewrite instructions.
To fix this, simply rename your directory (adding an underscore to the
beginning or end is a simple way to do that).
I have to take
http://www.mysite.com/index.php?mode=about
And have it show up as
http://www.mysite.com/about/
There are two very common types of rules that people want and your statement can be interpreted two ways which require different rules. I'm going to interpret your statement that you have a real, operational script at http://www.mysite.com/index.php?mode=about, but instead of having the user enter that "ugly" URL, you want them to be served that URL when they enter http://www.mysite.com/about/. To accomplish this, you would do the following:
RewriteRule ^about/?$ /index.php?mode=about [L]
Because of the potential for misunderstanding, it's best to state what you want as (1) What the user will enter into their browser and (2) what real file you want to serve them.
I don't believe you need lines #2 & 3 & you seem to have % instead of $, try:
RewriteEngine on
RewriteRule ^([^\/]+) /index.php?mode=$1 [L]
Solved the problem. Thanks for all the help.
#< IfModule mod_rewrite.c>
# RewriteEngine on
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
#< /IfModule>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?mode=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?mode=$1
You can use this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^([^/]+)/$ /index.php?mode=$1 [L]
Where the beginning still allows for other folders to be accessible.

URL Rewriting invisibly - How to prevent rewritten URL to appear in the address bar?

I have browsed the other topics, including this one: Mod_rewrite invisibly: works when target is a file, not when it's a directory, but I can't find a solution to my problem.
I have the following rewriting rule:
RewriteRule ^([a-zA-Z0-9_-]+)$ ./index.php?s=$1 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ ./index.php?s=$1 [L,NC]
What it does is to write anything like http://myaddress/parameter to http://myaddress/index.php?s=parameter and show this new rewritten address in the browser's address bar.
How can I make rewriting without showing the rewritten URL in the address bar?
Edit
This is the content of my .htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^([a-z0-9_\-]+)/?$ index.php?s=$1 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?$ index.php?u=$1&s=$2 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?$ index.php?u1=$1&u2=$2&s=$3 [L,NC]
1. No need for 2 rules that do the same job (the only difference is presence of trailing slash).
2. No need to have a-zA-Z in pattern if you have [NC] flag -- a-z is enough.
3. Try rule without ./
Considering all the above mentioned the rule will become:
RewriteRule ^([a-z0-9_\-]+)/?$ index.php?s=$1 [L,NC,QSA]
P.S.
I have also added the QSA flag to preserve original query string (if present).
The rule is tested and is working fine. If it still does not work for you then post ALL rewrite rules that you have.
This should work:
RewriteEngine On
RewriteRule ^([-a-zA-Z0-9_]+)$ index.php?s=$1 [L]
RewriteRule ^/?$ index.php [L]

mod_rewrite RewriteCond problem

In my .htaccess file I have the following rules:
RewriteEngine on
RewriteBase /myblog.com/
RewriteRule ^author/([^/]*)/?([^/]*)/?$ blog/author.php?author=$1&contactid=$2 [L]
RewriteRule ^blog/([^/]*[^/index.php])/?([^/]*)/?([^/]*)/?$ blog/index.php blog=$1&category=$2&article=$3 [L]
My problem is that a request like this myblog.com/author/Jim+Jones/28 is getting redirected to the following rule, even though I have a [L] flag there?
How can I exclude the second rule from firing when "/author/" appears in the URL?
Many thanks, Jason
/author/Jim+Jones/28 shouldn't get caught by any of these as neither of your rules catch URLs starting with / and the ^ at the start of you regex-es clearly stats that for the first rule it must start with 'author/' (note no / at the start).
UPDATE:
What I think is happening is that requests like myblog.com/author/Jim+Jones/28 are being rewritten twice. First by the first rule and then by the second one. If that's the case you'll need to make the 2nd rule more restrictive. Don't let it rewrite requests that have author.php in them for example.
UPDATE 2:
OK, so what would probably be the best in your case is to add a condition for the 2nd clause:
RewriteEngine on
RewriteBase /myblog.com/
RewriteRule ^author/([^/]*)/?([^/]*)/?$ blog/author.php?author=$1&contactid=$2 [L]
RewriteCond %{THE_REQUEST} !(author\.php)
RewriteRule ^blog/([^/]*[^/index.php])/?([^/]*)/?([^/]*)/?$ blog/index.php?blog=$1&category=$2&article=$3 [L]

How to reference the current directory from .htaccess using mod_rewrite?

I'd like to use mod_rewrite to make pretty URLs, but have a single version of the .htaccess file that can be used for any user on a server.
So far I have the standard pretty URL .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Ideally, I would like something similar to
RewriteRule ^(.*)$ %{URL of this file's directory}/index.php/$1 [L]
This documentation would lead me to believe that what I want is not necessary:
Note: Pattern matching in per-directory context
Never forget that Pattern is applied to a complete URL in per-server configuration files. However, in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done. This feature is essential for many sorts of rewriting - without this, you would always have to match the parent directory which is not always possible.
I still get the wrong result, though, whether or not I put a leading / in front of the index.php on the RewriteRule.
This,
http://server/test/stream/stream
turns into
http://server/index.php/stream
not
http://server/test/index.php/stream
when the .htaccess file is in /test/.
I was having a similar problem, but found that simply leaving off the root '/' resulted in my test web server (xampp on windows) serving a URL similar to:
http://localhost/C:/xampp/htdocs/sites/test/
Not perfect. My solution is to use a RewriteCond to extract the path from REQUEST_URI. Here's an example for removing the unwanted "index.php"s from the end of the URL:
RewriteCond %{REQUEST_URI} ^(.*)/index.php$
RewriteRule index.php %1/ [r=301,L]
Note: the percent sign in "%1/", rather than a dollar sign.
%1-%9 gets the patterns from the last matched RewriteCond, $1-$9 gets the patterns from the RewriteRule.
See:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond
Cheers :)
Turns out that it does matter whether I put a leading / in front of index.php or not. By leaving it off, the script works correctly. I had been testing with the R flag which was using physical directories on the redirect.

Resources