Mod_Rewrite for URL - mod-rewrite

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.

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.

301 URL Forwarding with HTACCESS or PHP

Just curious if anyone can help me on this HTACCESS issue.
I have these OLD URLS that need to get forwarded properly.
Previous structure
domain.com/Canada/Accounting
domain.com/Canada/Trades
domain.com/Canada/Sales
Proper structure
CATEGORY - /jobs/accounting-jobs
LOCATION - /jobs/jobs-kelowna
TOGETHER - /jobs/accounting-jobs-kelowna
Domain Structure
domain.com/jobs/[category]-jobs-[location]
Is this possible, either by HTACCES or PHP...just don't want these 404'ed pages.
I have 86+ to do, if there is a good way to forward these.
This is what I have, but i'm unable to successfully forward the bad-urls properly.
OLD
/browse
/Toronto/
/Canada/Administrative
/Vancouver/
/Canada/Trades
/Calgary/
/Canada/Hospitality
This is my HTACCESS right now.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#
# Trailing slash check
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
#
# PAGES
RewriteRule ^add-job/?$ /add-job.php [L]
RewriteRule ^jobs/?$ /results.php [L]
RewriteRule ^sitemap/?$ /sitemap.php [L]
#
# SEARCH
# CATEGORY - accounting-jobs
# LOCATION - jobs-kelowna
# TOGETHER - accounting-jobs-kelowna
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)?$ results.php?whatwhere=$1&page=$2
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)/?$ results.php?whatwhere=$1&page=$2
To 301 redirect your pages you can do something like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)/(\w+)$ /jobs/$2-jobs-$1 [R=301,L]
This only addresses the urls from your previous structure (the combinations, you have not shown any previous urls with just location or category) but note that Canada will stay Canada, it does not become canada. You can change everything to lower case using rewrite as well.
You also have to take care that you don't rewrite any of the current urls but without more information, this should do it.
Edit: For the location-only urls you could use a rule like:
RewriteRule ^(\w+)/$ /jobs/jobs-$1 [R=301,L]
Again, you need to look out that your rewrite rule does not interfere with your current urls. If that is the case, you would need to redirect every old url manually.
For lower-case new urls, you should search SO, there are some questions with good answers about converting a mized-case variable to lower-case.
If you have mod_rewrite, you can add these lines to your .htaccess file:
RewriteEngine on
RewriteRule ^Canada/Accounting$ /jobs/accounting-jobs [R,L]
However, it's not clear from your question exactly what you want mapped. Are the 3 previous URLs supposed to redirect to the 3 new ones? They don't seem to be equivalent.

RewriteEngine: subdomain to index.php, how?

Days later I asked about redirecting dynamic directories to index.php, and I got this code that works perfect (it's the only code I have in .htaccess):
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) index.php
This translates urls like http://mydomain.example/dynamicdir/ to http://mydomain.example/index.php
Now I want to translate subdomains like http://dynamicdir.mydomain.example to http://mydomain.example/index.php?dir=dynamicdir
From examples I found in Internet I tried adding this line:
RewriteRule ^(.*)\.mydomain\.example index.php?dir=$1
But it doesn't work. I don't have enough experience with mod-rewrite to tell what's missing or wrong. Could you please help me to find a way to keep the dynamic directory translation, and add the catch-all subdomain rule?
Regards!
The mod_rewrite rules use the request path, which is relative to the virtual host. Try having different rewrite rules for each virtual host, but placing their documents in the same directory.
With RewriteRule you can only test the URL path. For the host name, you need to use %{HTTP_HOST} in a RewriteCond:
RewriteCond %{HTTP_HOST} ^(.+)\.example\.example$
RewriteRule ^ index.php?dir=%1

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.

Mod-rewrite shenanigans

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.

Resources