how do I improve my simple mod_rewrite in htaccess? - .htaccess

I am using mod_rewrite for clean urls but I'm just having a small problem.
The line in particular is:
RewriteRule ^events/(.*)$ fe_events.php?event_url=$1
This works. I can go to url.com/events/something_or_other and it works as expected. The problem however is then if I just go to url.com/events it doesn't work - I've got to go to url.com/events/ (with a trailing forward slash on the end).
Can I simply modify this line or do I need to add another line such as:
RewriteRule ^events$ fe_events.php
Thanks for your time!

RewriteRule ^events(/(.*))?$ e_events.php?event_url=$1
this should work.

Related

RewriteRule not working asa expected .htaccess

Ok, so here's my RewriteRule:
RewriteRule ^books/book-([0-9]+)/(.*[^/])$/? authors/book-$1/$2 [R=301,L]
so if someone goes to www.example.com/books/book-5/some-book-title, they are redirected to www.example.com/authors/book-5/some-book-title. This bit works fine.
However, the problem I have is this:
www.example.com/books/book-5/some-book-title/still-more-stuff/still/more/stuff
it will forward to www.example.com/authors/book-5/some-book-title/still-more-stuff/still/more/stuff.
I want it to stop after www.example.com/books/book-5/some-title/ <- It should stop here!!!
It should not allow more parameters after the /some-title/ <- or it should disregard them.
How can I achieve this? Only grab the first parameter /some-title/, otherwise throw a 404? I thought that by putting the [^/] bit, it would stop when it found a forward /.
Thanks
Tweak your regex like this:
RewriteRule ^books/(book-\d+/[^/]+) authors/$1 [R=301,L,NE,NC]
Problem is actually presence of .* in your regex.

Awstats Redirection Using Mod_Rewrite and Issue with Colon Symbol

I have spent hours trying to get http://www.example.com/awstats to redirect to http://www.example.com/awstats/awstats.pl?config=example.com
The rewrite rule I used is:
RewriteRule ^https?://(www.)?(.*)/awstats/?$ /awstats/awstats.pl?config=$2 [QSA,L]
This doesn't work but it seems that it will work without the colon symbol.
I have tried using the HTML character code and escaping it but nothing I do seems to make any difference.
I have been testing it here: http://htaccess.madewithlove.be/
If I supply this value:
http//www.example.com/awstats
With this rule:
RewriteRule ^https?//(www.)?(.*)/awstats/?$ /awstats/awstats.pl?config=$2 [QSA,L]
Then it works but obviously I need it to process the colon symbol.
Please can someone tell me what I am dong wrong?
Thanks
Robin

.htaccess work with and without trailing slash

Using .htaccess, how can I get my website to be able to do this. Here is what I have already.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/(.*)$ sites/index.php?url=$1&page=$2
From this, I am able to be able to browse to:
http://domain.com/sites/example/
and that works totally fine.
What I want to be able to do is browse to
http://domain.com/sites/example
with no trailing slash, but it always gives back an error with page not found.
So far from Google, all of the results have failed me.
Thank you.
Try changing your regex to:
RewriteRule ^([^/]+)/(.*?)/?$ sites/index.php?url=$1&page=$2
The first (.*) is greedy, and will gobble up everything.
Yeah the sea of info in google can sometimes be a hinderance:
RewriteRule ^(.*)/(.*)/?$ sites/index.php?url=$1&page=$2
Note the /? in the rewriterule before the $, sorry I've read your question backwards...
edited answer:
RewriteRule ^(.*)/?(.*)$ sites/index.php?url=$1&page=$2
The different position of the ? might help as otherwise the greedy match of the first(.*) might glob to the end and try and shove that all into $1, for the specifics you'd be best turning on the apache rewrite log and looking at that if you can.

Mod_Rewrite to /subdirectory and /subdirectory/query

I'm having a difficult time getting into using mod_rewrite. I've been at this for about an hour googling stuff but nothing quite seems to work. What I want to do is change
example.com/species.php into example.com/species
and also
example.com/species.php?name=frog into example.com/species/frog.
Using
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^species/(.*)$ /species.php?name=$1
I can get example.com/species.php?name=frog to display as example.com/species/frog, and with
RewriteRule ^species/ /species.php
I can get example.com/species.php to display as example.com/species/, but I can't get both of them to work at the same time.
Also, example.com/species with no trailing slash always comes up as a 404.
I've considered just making a /species/ directory to catch any problems but I'd rather just have a few rules for one species.php file. Any help would gladly be appreciated!
Edit (because I can't answer my own question for 8 more hours):
I seem to have fixed both of my problems. I changed my .htaccess to:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^species/(.*)$ /species.php?name=$1
RewriteRule ^species/?$ /species.php
The second RewriteRule successfully redirects example.com/species to example.com/species.php while leaving the other RewriteRule working at the same time.
However, if I typed in example.com/species/ with a trailing slash, it was being read as example.com/species.php?name= and would throw an error because no name was submitted, so I just added
if(isset($_GET['name']) && empty($_GET['name'])) {header('location: http://example.com/species');}
so that if I used example.com/species/ it would redirect to /species and work as desired.
If you change the * (match zero or more) to a + (match one or more) in your first RewriteRule then you should stop seeing species.php?name= if a trailing slash is used.
This is because the + will require that something appears after the slash, otherwise the rule will not match. Then your second RewriteRule will match because it ends with an optional slash, but will not add the name= query string to the target URL.
You may also want to add the [L] flag (last) after the first rule, because you don't need the second rule to execute if the first rule matches. (Note that this will not stop the RewriteCond and RewriteRule tests being run on the resulting redirect URL, which will have to go through the .htaccess file just like any other request.)
See the Reference Documentation for mod_rewrite in Apache 2.4 (or see the docs for the version of Apache you're actually using).

htaccess rewrite, url doesn't work when i add a / at the end

I have got a problem that seems to me something simple but i'm new to htaccess and can't find the solution.
i've got the following line in my htaccess (root map)
RewriteRule ^page1/([a-zA-Z0-9_-]+)/$ page1.php?name=$1
RewriteRule ^page1/([a-zA-Z0-9_-]+)$ page1.php?name=$1
When i enter the the following url it works without a problem
www.myexample.com/page1/variable
the strange thing happens when I add a / at the end. Then the page can't get the GET value out of the URL.
Thanks for your time and help!
Get rid of the ending /$ sign on the first rule
RewriteRule ^page1/([a-zA-Z0-9_-]+) page1.php?name=$1
Or you can continue to capture data
RewriteRule ^page1/([a-zA-Z0-9_-]+)/(.*)$ page1.php?name=$1
Ultimately if you want to keep capturing more data with "/" as a delimiter, I suggest doing
RewriteRule ^page1/(.*)$ page1.php?url=$1
Then use the server side script to determine what to do.

Resources