Ok so I have a site that I want to write rewrite rules for. I don't have much experience with rewrites.
My .htaccess is in /foo/, not the root. Here's what I want
Case 1: example.com/foo/bar/ --> example.com/foo/includes/page.php?school=bar&id=
Case 2: example.com/foo/bar/1 --> example.com/foo/includes/page.php?school=bar&id=1
Here is what I have
RewriteEngine on
RewriteRule ^(bar|baz)/(.*)$ ./includes/pages.php?school=$1&id=$2 [NC,L]
There are only two possible values for school bar and baz. The id can have any number of values.
The above code works in Case 2 but doesn't work in case 1. It seems to externally redirect in case 1 to different urls depending on if there is a trailing slash or not. No idea why. Any help would be greatly appreciated.
You can put this code in your htaccess (in foo folder)
RewriteEngine On
RewriteBase /foo/
RewriteRule ^(bar|baz)/([0-9]*)$ includes/page.php?school=$1&id=$2 [NC,L]
Related
I'm having some trouble understanding .htaccess basically I'm trying to have more than one rewrite rule; for example I have a profile page and then I also want to rewrite the index page so how would this be done; in my previous attempts I could only use one rewrite rule perhaps I was doing something wrong; I have a profile page with the link /profile/profile.php?user=$username and the index page account.php?page=featured how could I get the profile page to look like /account/$username and the account page to look like /account/featured thankyou also how would I then add more later down the line?
the account.php file is in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^tag/([^/]+)/([^/]+)$ /tag/index.php?hash=$1&cat=$2
Options All -Indexes
ErrorDocument 403 Denied
that is my current .htaccess which uses the hashtags and shows them like this /tag/$hashtag when I tried to copy and paste this to then use under it it didn't work.
As mentioned in my comment, Apache won't be able to tell which file to rewrite to. So you'll need to change one of the URI structures. As a recommendation, change the one for the profiles.
Here is an example to show you how to do this:
RewriteRule ^tag/([^/]+)/([^/]+)$ /tag/index.php?hash=$1&cat=$2 [L]
RewriteRule ^account/([^/]+) /account.php?page=$1 [L]
RewriteRule ^profile/([^/]+) /profile/profile.php?user=$1 [L]
Remember the [L] flag, which causes rewriting to stop when a match is found.
I was wondering if anyone could help me with an htaccess rule.
I have a phone directory, and the folders are based on the phone number i.e. http://example.com/go/123-456-7890. But I'm finding a lot of users are refusing to use dashes and get an error message. I was wondering if there is a simple way to parse that with htaccess so they get automatically redirected to the right url format?
i.e. if they type http://example.com/go/1234567890 or http://example.com/go/(123)456-7890 that they get moved to http://example.com/go/123-456-7890
RewriteEngine On
RewriteRule ([0-9]{3})([0-9]{3})([0-9]{4}) /go/$1-$2-$3 [R]
This only does solid numbers - I am not great # mod_rewrite but this is a start:)
These 2 rules in your root .htaccess should take care of that:
RewriteEngine On
RewriteRule ^(go)/(\d{3})(\d{3})(\d{4})/?$ /$1/$2-$3-$4 [L,NC,R=302]
RewriteRule ^(go)/\((\d{3})\)(\d{3}-\d{4})/?$ /$1/$2-$3 [L,NC,R=302]
THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
I'm hoping someone can help with this one. I run a forum written in Perl, and the forum does something to URLs that is causing search engines to create duplicates.
I'm thinking that the best way of handling this is to sort it at the htaccess level.
As an example, the following 4 URLs all go to the same page, but search engines are seeing one entry with three duplicates:
http://www.domain.com/forum/YaBB.pl?num=1234567890
http://www.domain.com/forum/YaBB.pl?num=1234567890/2
http://www.domain.com/forum/YaBB.pl?num=1234567890/19
http://www.domain.com/forum/YaBB.pl?num=1234567890/22
I'm looking get htaccess to redirect anything that has a forward-slash somewhere in the last three characters, to a URL that has the slash and trailing numbers removed. Using the above example:
Redirect 301 /forum/YaBB.pl?num=1234567890/2 to /forum/YaBB.pl?num=1234567890
Alternatively, to re-write URLs from that subdomain to strip "/n" and "/nn"
Anyone have any ideas?
Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^cgi-bin/forum/YaBB\.pl\?num=([0-9]+)/[0-9]+$ cgi-bin/forum/YaBB.pl?num=$1 [R=302,L]
This should work, but let me know if it doesn't :) Also if it works, change the 'R=302' to 'R=301'
Try this rule:
Options +FollowSymLinks +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^num=(\d+)/
RewriteRule ^(cgi-bin/forum/YaBB\.pl)$ /$1?num=%1 [R=301,L]
Tested on local Apache installation -- works fine for me.
EDIT: Having issues with the code below
Okay, I'm getting a page that says "The page isn't redirecting properly". This is if someone tries to access /files/protected/file.jpg. I'm trying to redirect it to /myfiles/file.jpg, but instead I get that error...
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /myfiles/
RewriteRule ^(.*)$ $1 [L,R=301]
</IfModule>
Essentially, I'm trying to figure out how to add a rewrite condition that will retain part of the path. If a user tries, for example, to access a file within a directory, it redirects to another url with that filename as a parameter.
So, if a user visits: mysite.com/protected/file.pdf, it will redirect to mysite.com/okay/file.pdf
Is that something that I can use .htaccess for?
You need to put your .htaccess file in your site root. Then, assuming you want an external redirection, it should contain the following:
RewriteEngine On
RewriteRule ^files/protected/(.*)$ myfiles/$1 [R=301,L]
Your current rule always matches, so it performs the redirect an infinite number of times, and you end up with that error.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
You want the part about RewriteRule backreferences with regex groups referred to by $N.
For your example, I think this is what you've intended:
RewriteRule ^(mysite\.com/)protected(/.*) $1okay$2