Ignored mod_rewrite instructions in .htaccess (WordPress) - regex

We've changed the permalink structure for our WordPress blog (bad idea, I know). I tried to avoid 404s from external links with mod_rewrite instructions in the .htaccess of the base folder of the WordPress installation:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^\d\d\d\d\-\d\d\-\d\d_(.+)$ $1 [L]
# other rewrites
</IfModule>
I tried a few variations on that, even testing the full URL of one page, but the line keeps being ignored. But mod_rewrite is running - some other rewrites like
RewriteRule ^seiten/abo\.php[5]?$ subscribe.php5 [R=301,L]
work perfectly. I'm stuck. Can anybody help?

Try forcing a 301 redirect by adding R=301 flag in the square brackets:
RewriteRule ^\d\d\d\d\-\d\d\-\d\d_(.+)$ $1 [L,R=301]
This will make it so other rewrites won't (after the first rewrite iteration) modify the URI.

Related

Exlude a directory in .htaccess with RedirectMatch for Wordpress

I change my Wordpress permalink structure from %year%/%months%/ to %postname%
So i create this mod alias rules in .htaccess but i have a problem:
RedirectMatch 301 /\d{4}/\d{2}/([^/]+)(/?)(.*)$ http://domainname.com/$1
The rules also redirect the images under the wp-content directory, so domainname.com/wp-content/uploads/2013/11/name.jpg become domainname.com/name.jpg
I can't use RewriteCond because this RedirecMatch is mod_alias, i try to use !^/(wp-content.*)$ but doesn't work.
How can i solve? It's better to use RedirectMach or RewriteRule (i haven't changed the server)?
Can you post a better regex?
Thank you!
You should probably stick with using mod_rewrite instead of mod_alias because it'll interfere with wordpress' mod_rewrite rules. Both mod_rewrite and mod_alias affect the same request URI at different points in the URL-file processing pipeline, so you could end up getting redirected and rewritten at the same time.
RewriteCond %{REQUEST_URI} !\.(jpeg|gif|png)$ [NC]
RewriteRule ^\d{4}/\d{2}/([^/]+?)(/?)(.*)$ http://domainname.com/$1 [L,R=301]
Try ^ and $:
RedirectMatch 301 ^/\d{4}/\d{2}/([^/]+)(/?)(.*)$ http://domainname.com/$1

.htaccess RewriteRule to path without changing URL

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.

Rewrite all requests through a directory

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]

.htaccess rewrite not doing anything

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]

Redirecting from a domain to a specific url of a Django project

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!