.htaccess ReWriteRule - Quick Question - .htaccess

I've created a ReWriteRule, which I think points to claim.php?claimlisting=$#
The rule is written as follows :
# Claim
RewriteRule ^listing/claim/([a-zA-Z0-9-]+).(.html|htm)$ claim.php?claimlisting=$1 [L]
An example link is : /listing/claim/anne-morrison--hypnotherapy-cognitive-behaviour-therapists
I would like my MOD_REWRITE to point to the ROOT dir and claim.php?claimlisting=$ID_HERE
I've put the file up, But it doesnt seem to be affecting the site at all? Is what I've done correct and what else would be a factor preventing it from functioning correctly?
Thanks

# Claim
RewriteEngine On
RewriteRule ^listing/claim/([a-zA-Z0-9\-]+).(html|htm)$ /claim.php?claimlisting=$1 [L]
And you are missing .html at the end of link

You have an extra period.
RewriteRule ^listing/claim/([a-zA-Z0-9-]+)\.(html|htm)$ claim.php?claimlisting=$1 [L]
I removed the one in the second set of parenthesis, and escaped the first one. Try that.

Related

Modrewrite for pretty url

How can modrewrite be done in this above situation:
Lets say the website us is: www.real-estate.com
Then we have the first mod rewrite:
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So this will rewrite to something similar: www.real-estate.com/florida and it will list all the real estates in florida.
Then we add this rule:
RewriteRule ^([a-zA-Z\-]+)/(.*)+$ details.php?project=$2
This will rewrite to www.real-estates/florida/project-one and it will display details from that project.
But if I access the link for the city like this: www.real-estae.com/florida/ (with slash last) it will jump me to the second mod rewrite rule, taking me to details.php with an empty variable.
What is the correct solution to slove this problem, so that if the users adds a slash after the city it will still display the projects in that city and not the details page with empty var ?
After playing around I found that his works, but I do not know if it is the correct solution:
I replaced this:
RewriteRule ^([a-zA-Z\-]+)/+(.*)$ details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
With this:
RewriteRule ^([a-zA-Z\-]+)/([a-zA-Z\-]+) details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So I have replaced (.*)$ Why does this work and the other way not I do not know.. Maybe someone can explain.
Probably it fixed the immediate problem but your rules might still match more than required due to faulty regex used and not using anchor $.
For your case use these rules:
RewriteEngine On
RewriteBase /
RewriteRule ^[a-zA-Z-]+/([a-zA-Z-]+)/?$ details.php?project=$1 [L,QSA]
RewriteRule ^([a-zA-Z-]+)/? cities.php?city_url=$1 [L,QSA]
Apache mod_rewrite Introduction

my .htaccess rewriteRule is failing

I'm having trouble understanding why this rewrite isn't doing what its told.
NOTE: the first rewrite in my .htaccess file works properly so its not a problem with using mod_rewrite on local host.
i have URIs which i know will be in the format:
http://localhost/managerhub/my-manager.php?i=1&t=dashboard
when site goes live:
http://themanagerhub.com/my-manager.php?i=1&t=dashboard
my .htaccess file reads thus:
RewriteEngine On
RewriteRule ^([a-z\-]+)$ $1.php [L]
RewriteRule ^my-manager-([0-9]+)-([a-z]+) my-manager.php?i=$1&t=$2 [PT]
To achieve clean URls like:
http://localhost/managerhub/my-manager-1-dashboard
Ideally i dont really want the first capture group ([0-9]+) since i dont really want the 'i' value in the resultant
clean url - so ideally id like:
http://localhost/managerhub/my-manager-dashboard
However ive not even got the rewrite to work so far at all having tried:
leading forward-slash on the target (though i dont think it was necessary)
tried changing the '&' ampersand in the target to use &
removing the [PT] passthru flag replaced with and without [L] flag
tried most 'least' restrictive character classes in the pattern i.e. (.*) instead of ([0-9]+)
commented 'out' the first RewriteRule which works flawlessly BTW - so using the troublesome rule in isolation
Non of these have worked - the second rewrite rule has no effect on the target urls so i cant even see were the discrepancy is. I'm still new to mod_rewrite so sort of rely on an informative fail so i can work out were my reg-ex is wrong but i suspect its just being ignored since im getting 'zilch' back!!
Any help appreciated - maybe with a pointer to my folly.
thanks
Your htaccess file, I'm assuming, is in the "managerhub" directory. That's where those rules need to be. You may need to add a base as well:
RewriteEngine On
RewriteBase /managerhub/
RewriteRule ^([a-z\-]+)$ $1.php [L]
RewriteRule ^my-manager-([0-9]+)-([a-z]+)$ my-manager.php?i=$1&t=$2 [L,PT]
which you'd need to change when they get to the live site. You can remove the first capture group via:
RewriteRule ^my-manager-([a-z]+)$ my-manager.php?t=$1 [L,PT]
The L flag isn't exactly 100% needed.

php to html .htaccess Help

I have the following code in the my .htaccess file and the top Rewrite works fine the bottem one does not I know why but I dont kno how to fix it.
Its seeing RewriteRule ^([^/]*).html index.php?p=order&course_id=$1 [L] as the top rewrite command becuase of the hightlighed part and i dont want to put it in a dir
RewriteEngine on
RewriteRule ^([^/.]+).html
index.php?p=$1 [L]
index.php?p=about_us
RewriteRule ^([^/]+).html
index.php?p=order&course_id=$1 [L]
index.php?p=order&course_id=5
Thank you,
Can you give example urls that should match the pattern block and what you would like them to be rewritten to? That would be very helpful.
One thing I notice is that your first regexp you test if you match the pattern block with a + which means 1 or more times and the second one you check it with a * which means 0 or more so I don't think the second one will ever be called, although I am very new to regexps but it is just something I noticed.
These are very helpful resources for me:
http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
http://forum.modrewrite.com/
From the example of the urls you would be using, this should work:
# http://website.com/about_us/ rewrites to /index.php?p=about_us
RewriteRule ^([0-9a-z_-]+)/?$ index.php?p=$1 [NC,L]
# http://website.com/order/12/ rewrites to /index.php?p=order&course_id=12
RewriteRule ^order/([0-9]+)/?$ index.php?p=order&course_id=$1 [NC,L]
The second Rewrite might be:
# http://website.com/order/12/ rewrites to /index.php?p=order&course_id=12
RewriteRule ^([0-9a-z_-]+)/([0-9]+)/?$ index.php?p=$1&course_id=$2 [NC,L]
Depending on your page structure.

get the index.php with .htaccess

I'm using a bunch of ReWrite rules in my .htaccess such as these
RewriteRule ^brochure/([^/]+)/?$ brochure.php?cat_path=$1
It's all working great thanks to answers I found by searching this site. But I've got a tiny little snag.
This rule works perfecty at the moment
RewriteRule ^([a-zA-Z0-9_-]+)?$ $1.php It catches all my urls like www.mysite.com/shane and requestes shane.php - So far so good, but when I call www.mysite.com I need it to request index.php
Which rule could I use for this?
Thanks people.
UPDATE - For the sake of future users needing similar help.
My rule of ^([a-zA-Z0-9_-]+)?$ $1.php was causing the problem I mentioned above. The question mark ? is not needed in this type of rule, when you remove the question mark the index.php file in the directory returns to being the default landing page. Thanks to Tomalak for the Help.
You don't need any rule for it. Just make sure that .php default index files for the directory and it'll happen automatically.
Update
I think that your rule
^([a-zA-Z0-9_-]+)?$ $1.php
should in fact be
^([a-zA-Z0-9_-]+)$ $1.php
so that it does not conflict with requests where the filename isn't given (which you want to direct to index.php).
I think this is what you're looking for,not sure thought:
RewriteRule ^/$ index.php
You can either do as Tomalak says, and ensure the DirectoryIndex is set to load index.php.
Or close to what James says,
RewriteRule ^/?$ index.php [L]

Problem with rewrite rule

Can anyone explain to me why this rewrite rule doens't work:
RewriteRule ^architecture/([a-zA-Z_]+)/(.*).html$ web/index.php?field=1&sub=$1&name=$2
on this url: http://localhost/greenlinked1-6.com/architecture/projects/84-test-deeplink-test.html
And what I should do to get it working.
I've tried to find the answer in several articles but I can't figure it out.
Your .htaccess file should be placed in the directory greenlinked1-6.com. Make sure there are no conflicting rules, like this:
RewriteRule ^(.+)$ $1 [L]
# This line will never be be matched
RewriteRule ^architecture/([a-zA-Z_]+)/(.*).html$ web/index.php?field=1&sub=$1&name=$2
check that u havn't used RewriteBase / just remove & try again...

Resources