I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.
Related
I have the following URL:
http://example.com/pages/page.php?company_name=Name
What I want to achieve is to have a URL like this:
http://example.com/pages/Name
I have tried different rules but they don't work:
RewriteRule ^pages/([A-Za-z0-9-]+)/?$ /pages/page.php?company_name=$1 [NC] or
RewriteRule ^pages/([^/]*)\.php$ /pages/page.php?company_name=$1 [L]
It doesn't work. It gives me a "not found" page. How can I properly use mod_rewrite?
Try this out:
RewriteRule ^pages/([0-9a-zA-Z\-_]*)(/|)$ /pages/page.php?company_name=$1 [QSA,L]
This will care or not care if the url has a trailing slash:
http://example.com/pages/Name
http://example.com/pages/Name/
And will also include any extra agrs (QSA) if the page calls for it:
http://example.com/pages/Name/?more=stuff
That should work, if your apache has mod_rewrite and the php exists where you have shown.
UPDATE
If you have a different url that needs a different php, for example 'bluepages':
http://example.com/bluepages/Name
Then this would work for that:
RewriteRule ^bluepages/([0-9a-zA-Z\-_]*)(/|)$ /bluepages/somescript.php?some_var=$1 [QSA,L]
If you have multiple like this you wish to control, you can make multiple RewriteRules in your htaccess for each one. However if you just want to wildcard it, then this would do a blind catchall (and cause lots of error reports in your apache logs):
RewriteRule ^([0-9a-zA-Z]*)/([0-9a-zA-Z\-_]*)(/|)$ /$1/page.php?some_var=$2 [QSA,L]
You will either need to be specific, or just change everything. There are so many ways one can go with it, and it really depends on your intent.
so I have this htaccess entry:
RedirectMatch /([a-zA-Z0-9]+).php /dirA/$1.php
The goal is that any .php that is on the root directory should be redirected to /dirA/*.php
eg. suppose I make the request
domain.com/something.php
it should instead redirect to
domain.com/dirA/something.php
However when I put that entry in my .htaccess file and then I go to domain.com/something.php
it instead returns
"The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for
this address in a way that will never complete."
Any idea how I can modify my htaccess to accomplish what I want to do?
Updated Question
Also is there a way to make it so that it only redirects if the file doesn't exist in the root directory...hence if x.php exists in root, serve that x.php otherwise redirect to dirA/x.php
mod_rewrite is an overkill for this, you were on the right track with RedirectMatch. Your rule, however, is a bit faulty: the regex /([a-zA-Z0-9]+).php matches all string that contain the specified substring, so it matches "/foo/bar/baz.php", but also "dirA/foo/bar.php" (and even "/foo/bar.php/baz.php"I. Your redirection ended up in an endless loop because there was no stop condition: /dirA/foo.php was redirected to /dirA/foo.php.
You can remedy the situation by using anchors in the regex:
RedirectMatch ^/([a-zA-Z0-9]+).php$ /dirA/$1.php
As for your second question: that might indeed call for mod_rewrite. Something along these lines should do the trick:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+).php$ /dirA/$1.php [R=301]
I haven't tested it, but this should get you started. Make sure to check out the manual for details, or just search around on SO, there are tons of questions about this.
Try this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+).php /dirA/$1.php [R=301,L]
This one should be just fine
RewriteRule ^(.*)$ /subdir/$1
I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?
i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.
I've been searching all over the web and haven't yet found any solution to this issue. I'm hoping you could shed some light on the situation.
I have my index file set up like this:
<header></header>
<div id="main">
<?php
if(isset($_GET["p"])) $p = $_GET["p"];
else $p = "home";
if(file_exists("pages/{$p}.php")) include("pages/{$p}.php");
?>
</div>
which makes me load my pages with a ?p=contact href.
Say I would like to display a users profile. I'd then create a subfolder in my "pages" folder, making the relative path pages/users/profile.php, thus the href ?p=users/profile&uid=5. But that leaves an ugly URL (as well as SEO rating).
How would I rewrite that URL to look like /users/profile/5?
EDIT:
I've tried the following, resulting in HTTP 500:
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&uid=$2 [L]
EDIT: My .htaccess file, located directly inside root folder: http://pastie.org/2268239
Line 338 is where I want to achieve this (currently just a comment).
Simplest answer for both your situations would be to add this in your .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php?$1 [L]
This will redirect all traffic on your domain to your index.php file.
You could then determine what to do in your script using the uri in $_SERVER["REQUEST_URI"]
I achieved the desired effect by adding these three lines:
RewriteRule ^([a-zA-Z]+)$ index.php?p=$1 [L]
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)$ index.php?p=$1/$2 [L]
RewriteRule ^([a-zA-Z]+)/([0-9]+)$ index.php?p=$1&uid=$2 [L]
This allows me to access /contact, /users/index and /users/profile/5.
I'm not a php guy, but I tried this with my rewrite.
RewriteRule ^/(.+)/([^/]+)$ /index.php?p=$1&uid=$2 [L]
In this case, for the p parameter, you're looking for all chars up to the last slash, so the first part of this takes anything, otherwise it's going to stop at the first slash (users instead of users/profile).
Then it looks for a slash and keep (not-slash). The (.+) will be greedy, so it will go up to the last slash before the end.
Then it occurred to me the last part doesn't need to avoid slashes. Since the first part is greedy, the explicit / slash is going to BE the last slash. So it's even simpler:
RewriteRule ^/(.+)/(.+)$ /index.php?p=$1&uid=$2 [L]
I like the .+ to require something, at least when first figuring these out. If later you know they can be optional, you can do .*, but usually that ends up being a different page or a different rule.
These rules do expect all urls to be in this format, which is what you're asking. But maybe it's a little too grabby, so it could exclude urls that really have a .htm or .php or whatever.
RewriteRule ^/(.+)/([^.]+)$ /index.php?p=$1&uid=$2 [L]
This looks for anything up to the last slash, then anything without a dot in it. If it has a dot, this won't apply. So if it's a "regular" url, this will leave it alone. This might help with the 404 problem, in case the 404 page is getting caught by this.