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.
Related
I currently have the following .htaccess rewrite rule setup on my site:
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.html?id=$1 [QSA]
The rule works in such a way that if I go to the following URL:
http://example.com/dashboard
It won't try and find the dashboard directory that doesn't exist but instead it will keep the URL as is and redirect the user to the root index page. From there I just use javascript to control what view the user will see depending on what path is appended.
The code works exactly as I want it to but i've now had to move my site into a sub-directory on our server. The URL structure is now this:
http://example.com/mysubdir/dashboard
I tried rewriting my rewrite rule to incorporate the directory but have not been successful so far as i'm no .htaccess expert. I tried something along the likes of:
RewriteEngine On
RewriteRule ^([^/d]+)/mysubdir/?$ index.html?id=$1 [QSA]
Could anyone tell me how I can amend my rewrite rule to work in my sub-directory?
You were close - this should do it:
RewriteEngine On
RewriteRule ^mysubdir/([^/d]+)/?$ /mysubdir/index.html?id=$1 [QSA]
Demo here: http://htaccess.mwl.be?share=6e574cc6-90a4-54ca-b113-ce72d6eb5203
Here is a sample of filter URL's on an ecommerce store.
http://www.domain.com/showering/showers/filter/alliance
http://www.domain.com/showering/showers/filter/aquaflow
http://www.domain.com/showering/showers/filter/grohe
http://www.domain.com/showering/showers/filter/mira
I'm wondering if there is a way I can mask these URL's so they appear like:-
http://www.domain.com/alliance-showers
http://www.domain.com/aquaflow-showers
http://www.domain.com/grohe-showers
http://www.domain.com/mira-showers
But still display the page content from the /showering/showers/* URL's?
I then wish to be able to set the canonical URL's based on these masked URL's.
I've played around with countless variations with little success but here is something I've got so far:-
RewriteEngine on
RewriteCond %{HTTP_HOST} !^/showering/showers/filter/(alliance)
RewriteRule (.*) /alliance-showers/
When this is applied to website, all the images on the Magento store don't load incidentally along with the fact that the URL doesn't change at all.
Answer to #anubhava's comment...
.htaccess file is in root or Magento installation. It is the very first rule in file like so:-
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^-]+)-([^/]+)/?$ /showering/$2/filter/$1 [L,R]
Currently testing with this URL:-
http://www.showermania.co.uk/showering/showers/filter/alliance
Wanting to show as:-
http://www.showermania.co.uk/alliance-showers
Current answer has no affect/change on this URL at all. Thanks.
You can use a rule like this:
RewriteEngine on
RewriteRule ^showering/([^/]+)/filter/([^/]+)/?$ /$2-$1 [L,NC]
I have the following htaccess lines...
RewriteRule ^([a-zA-Z0-9]+)?$ index.php?p1=$1 [L]
RewriteRule ^~([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$ ~$1/index.php?p1=$2 [L]
The first line works fine, its the second line that doesn't work at all...
The first line does this....
domain.com/about -> domain.com/index.php?p1=about
What I'm trying to do with the second line...
if the url is server1.domain.com/~username/about....
I need it to translate to server1.domain.com/~username/index.php?p1=about
Basically, detecting if there is a ~
I am trying to work out my code to allow for the development url of the hostname/~username
Right now it is showing the green apache 404 not found page when trying to visit the website using that code.
Please let me know if you need any more information
Switch the order of the rules and add a RewriteBase:
RewriteBase /
RewriteRule ^~([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$ ~$1/index.php?p1=$2 [L]
RewriteRule ^([a-zA-Z0-9]+)?$ index.php?p1=$1 [L]
You need to be sure that this .htaccess file is triggered both on requests to pages from domain.com and pages from server1.domain.com.
Also, if you have a .htaccess file in any subdirectories (you shouldn't, based on your problem description), you will have to modify those accordingly, but we would need more information.
You need to place this rule in your httpd.conf:
RewriteEngine On
RewriteRule ^/?(~[a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /$1/index.php?p1=$2 [L,QSA]
And make sure this line is uncommented in httpd.conf:
LoadModule userdir_module modules/mod_userdir.so
Then make sure index.php is present directly under ~username/
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]
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