I have a page with the URL of http://www.freshmarketstores.com/departments/produce/recipe.php?ing=Akane_Apples&recipe=59 that I would like to be cleaned up to look like http://www.freshmarketstores.com/departments/produce/recipe/Akane_Apples/59
Right now, in my .htaccess file that is located in the produce folder, I have the following code:
RewriteEngine on
RewriteRule ^recipe/(.*)/(.*)$ recipe.php?ing=$1&recipe=$2 [L]
Currently, this does not affect anything. when the recipe.php?ing=Akane_Apples$recipe=59 url is loaded, it loads fine, but does not do the rewrite. This is my first attempt at URL rewriting and can't figure this thing out.
Thanks in advance for your help.
EDIT:
Okay, so after some learning last night, I realize I was approaching this backwards. I am able to type in http://www.freshmarketstores.com/departments/produce/recipe/Akane_Apples/59 and get the resource that is stored at .../produce/recipe.php?ing=Akane_Apples&recipe=59. Thanks for all of your help on that.
My question now, is can you go the opposite way? If I type in .../produce/recipe.php?ing=Akane_Apples&recipe=59, how can I get it to redirect to the /produce/recipe/Akane_Apples/59?
Ensure you have the
AccessFileName .htaccess
setup in your Server and virtual host configuration files in apache, the default is setup to be .htaccess however it is not restricted to only that and can be named anything.
default config file for a debian based system is /etc/apache2/apache2.conf and for most other systems httpd.conf in the apache install directory
once the access file is setup and you have mod_rewrite enabled you can do something as below
http://www.freshmarketstores.com/departments/produce/Akane_Apples/59
RewriteEngine On
RewriteRule ^recipe/([^/]*)/([^/]*)$ /recipe.php?ing=$1&recipe=$2 [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# internal forwarding to /recipe.php
RewriteRule ^recipe/([^/]+)/([^/]+)/?$ recipe.php?ing=$1&recipe=$2 [L,QSA,NC]
# external redirect to /recipe
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+recipe\.php\?ing=([^&]+)&recipe=([^\s]+) [NC]
RewriteRule ^ recipe/%1/%2? [R=301,L]
You should be okay with
RewriteEngine on
RewriteCond %{THE_REQUEST} \?ing=([^&]+)&recipe=([^&]+) [NC]
RewriteRule ^(departments/produce/recipe)\.php$ http://%{HTTP_HOST}/$1/%1/%2 [NC,R=301,L]
Related
My goal is to force HTTPS on the admin directory of my site. However, using the usual way give an unusual result. Here is the content of .htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} admin
RewriteRule ^(.*)$ https://www.domain.com/admin/$1 [R,L]
However, when I try to open http://www.domain.com/admin/home.php, instead of redirecting to https://www.domain.com/admin/home.php it sends me to https://www.domain.com/admin/admin/home.php.
So it sort of creates the admin/ part of the URL twice.
I tried changing the last line of .htaccess to RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L] (which doesn't make sense to me, but tried it still) and it redirected to the same wrong URL, which confused me further.
I'm not sure if it should matter, but I've also tried replacing [R,L] with [L,R=301] as suggested elsewhere on SO, with no avail.
Why is this happening and what's the solution?
It is due to /admin/ in your target URL. Change your rule to this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^admin(/.*)?$ https://www.domain.com%{REQUEST_URI} [R,L,NC]
So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.
My site www.example.com has its document root to public_html directory. But, all of my content is hosted in public_html/www.example.com/. So, I want when a user visits my site he get contents from public_html/www.example.com/ and not public_html. So, what should be the .htaccess rewrite rule for that?
I am not good in rewrite rules but the best I found was:
RewriteRule !^blog blog%{REQUEST_URI}
There are two problems with this.
It will not work properly if user requested www.example.com/blog/
It will also not work when we use directory name as www.example.com
Give this a try and let me know if it works for you, and if it doesn't try to describe as detailed as possible what happens.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/www\.example\.com [NC]
RewriteRule ^(.*)$ /www.example.com/$1 [NC,L]
I own two domains example1.com and example2.com. I setup example1.com to point to the the "/" of my Django project, meaning that example1.com/* is handled by urls.py. I want example2.com to actually point to a specific Django URL (e.g. "/123"), which is currently accessed at example1.com/123.
Ideally, when you go to example2.com/abc, Django would treat the request as if it came from example1.com/123/abc, but the URL would still be "example2.com/abc".
Here's what I have currently in my .htaccess file, but example2.com leads to "Server not found" in Firefox.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule ^\.com(.*)$ http://example1.com/123/$1 [L]
This is my first experience doing something like with the htaccess file, so apologies if something is off. I am using mod_wsgi (on WebFaction)--does this still support rewriting URLs? Thank you for your help.
Try adding the following to your htaccess file in the root directory of your example2.com site.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
#proxy all requests to example1.com/123
RewriteRule (.*) http://example1.com/123/$1 [P]
I needed to edit httpd.conf, as opposed to .htaccess. Here are the relevant lines:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)?example2.com$ [NC]
RewriteRule ^(.*)$ http://example1.com/123/$1 [P,L]
These lines go directly beneath the WSGIScriptAlias that directs to the Django project .wsgi file. I don't know why the .htaccess file did not do the trick, but hopefully this helps out others with the same problem.
Thanks!
Hey, I am trying to use mod_rewrite to make http://example.com/style/universal.css show the page http://example.com/style.php?n=universal
I am currently using the following RewriteRule:
RewriteRule ^style/([^/\.]+)\.css$ style.php?n=$1
But it doesn't seem to work, for some reason it instead shows a 404 Not Found which is not even the correct 404 page.
I have tried doing it without the extension .css (i.e. http://example.com/style/universal) with the RewriteRule
RewriteRule ^style/([^/\.]+)$ style.php?n=$1
Which works, but I would much prefer it if I could get it working with the .css extension.
It seems to be something with the server ignoring the RewriteRule if a file extension is used. Is there a RewriteCond or something obvious that I am missing?
I should also mention that there is a .htaccess in the parent directory which is set by WordPress, the contents of this are:
RewriteEngine on
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any help would really be appreciated.
Thanks in advance, Brad.
Set up your mod_rewrite to log all the steps it does processing your url
RewriteLog "/tmp/mysite_rewrite.log"
RewriteLogLevel 6
reading the logs you should be able to understand how your url is rewritten and what is the problem.