Apache mod_rewrite alias a path to another path - apache

I have a weird rewriting problem due to some Drupal issue. I know not how to fix the Drupal issue, so this will be a temporary fix:
The path /the%20campanile/([0-9]+)/([0-9]+)/ (preferably with slash optional) should be an alias for /campanile/([0-9]+)/([0-9]+)/.
I've tried:
RewriteBase /
RewriteRule ^campy/([0-9]+)/([0-9]+)/$ campanile/$1/$2/
except that fails with 404 (presumably rewriting wrong). I've also tried with wildcards (*), but to no avail. I looked through about 16 posts on StackOverflow and a few other forums.
The entire .htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]
RewriteRule ^campy/([0-9]+)/([0-9]+)/$ campanile/$1/$2/
RewriteCond %{HTTP_HOST} ^danger-\.palyvoice\.com
RewriteRule ^(.*)$ /category/blogs/danger-zone [L]
What am I doing wrong?

Not sure why you're regular expression is ^campy/([0-9]+)/([0-9]+)/$ when you're trying to match something like /the%20campanile/([0-9]+)/([0-9]+)/. Groupings aside, the campy will never match the%20campanile.
You need to keep in mind that the URI is decoded before being processed through mod_rewrite, so you don't want to match against %20, but just the space:
RewriteRule ^/?the\ campanile/([0-9]+)/([0-9]+)/?$ campanile/$1/$2/ [L]
You also want these rules before your index.php routing rule, since the routing rule will rewrite it before it gets to your other rules:
RewriteEngine on
RewriteBase /
RewriteRule ^/?the\ campanile/([0-9]+)/([0-9]+)/?$ campanile/$1/$2/ [L]
RewriteCond %{HTTP_HOST} ^danger-\.palyvoice\.com
RewriteRule ^(.*)$ /category/blogs/danger-zone [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]

Related

.htaccess redirect based on folder

I need help with .htaccess Rewrite rules.
I have a API which can be accessed over http://api.my.domain/products/all which is working fine and is returning result.
I would like to redirect users coming to http://api.my.domain/admin to admin folder.But it is not working with current rules.
I have added this to .htaccess but it is not working correctly for admin folder.
RewriteEngine On
RewriteCond %{Request_Filename} !-F
RewriteCond %{Request_Filename} !-d
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^admin admin/index.php [QSD,L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ public/index.php [QSD,L]
This is the result I get when I enter http://api.my.domain/admin and is breaking all my php redirects:
http://api.drezga.hr/admin/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/admin/admin/checklogin.php
Can someone, please, tell me what I'm doing wrong? I have spent hours and I can't see it.
Aside: If /admin is a physical directory then you should be requesting /admin/ (with a trailing slash) to begin with, otherwise Apache/mod_dir will issue a 301 redirect to append the trailing slash.
RewriteCond %{Request_Filename} !-F
RewriteCond %{Request_Filename} !-d
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^admin admin/index.php [QSD,L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ public/index.php [QSD,L]
The first two conditions (RewriteCond directives) are being applied incorrectly to the first rule only. They need to apply to the last rule (the rewrite to public/index.php) then the rewrite to admin/index.php is not necessary (this should be handled by the DirectoryIndex).
There's no need for the QSD flag since you are checking that the query string is already empty - there is no query string to discard!
You should probably be using the -f operator on the condition, not -F (which uses subrequests and is consequently less efficient).
Try the following instead:
DirectoryIndex index.php
RewriteEngine On
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Optimisation (prevent additional filesystem check)
RewriteRule ^public/index\.php$ - [L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . public/index.php [L]
RewriteRule ^$ public/index.php [L]
The RewriteCond %{REQUEST_FILENAME} !-d directive prevents requests for /admin/ being passed to public/index.php.
The additional RewriteRule at the end is to rewrite requests for the root directory, which would otherwise be omitted because of the condition mentioned above. This could be avoided by extending the DirectoryIndex directive instead, although this could change the behaviour if you have other directories that need to be accessible (or should not be routed to public/index.php). For example:
DirectoryIndex index.php /public/index.php

Mod Re-Write Struggling With Rule Not Working

So after a lot of testing - I've started to figure out my problems but still can't get the rewrite to work.
This is how my htaccess file looks now.
RewriteEngine On
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Rule 1 is the one I'm having problems with
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
My understanding is now if I type this url into my browser
https://abfielder.com/schemdetail/id/158
I should get
https://abfielder.com/schematicdetails?id=158
The test of this here
https://htaccess.madewithlove.com/
Tells me my rules are ok.
And rules 2 and 3 are working fine.
However when I try and access
https://abfielder.com/schemdetail/id/158
I essentially get the page not found error.
Ok think I've finally fixed this for anyone else who is having issues with mod rewrite where they wish to have a rule to remove the file extension and other rewrites working together the order matters. Lastly if you're on a shared host type thing you may also need to set the rewrite base. This is how my htaccess file looks now.
RewriteEngine On
RewriteBase /
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
This allows me to have url's without the .php extension.
It also allows for
schemdetail/id/100 to be translated into schematicdetails?id=100

Why isn't my htaccess RewriteRule working for my query strings?

I am moving an ASPX site to Wordpres -- from an IIS server to Apache -- and I am having problems getting the pages with query strings to redirect. I looked into the Apache documentation and am trying to use mod_rewrite, but so far no luck.
For example I would like to redirect these pages:
www.mysite/Product/Product.aspx?mapid=324$ to
www.mysite/blue-widgets/widget7/
www.mysite/Product/Product.aspx?mapid=681$ to
www.mysite/blue-widgets/widget12/
www.mysite/Product/Product.aspx?mapid=841$ to
www.mysite/blue-widgets/widget82/
Here is where I am at. I have tried several iterations of this but can't get it to work. I am starting to think that it might have something to do with first few Rules, which were generated by the server and Wordpress. The site is an addon domain, which the server treats like a subdomain.
My rewrites are the last three. Any ideas why these aren't working? Thanks for any help.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~username/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~username/mysite/index.php [L]
RewriteCond %{QUERY_STRING} ^mapid=324$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget7/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=681$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget12/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=841$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget82/? [L,R=302,NC]
</IfModule>
I believe the following will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mapid=324$ [NC]
RewriteRule /Product/Product\.aspx https://www.example.com/blue-widgets/widget7/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=681$ [NC]
RewriteRule /Product/Product\.aspx https://www.example/blue-widgets/widget12/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=841$ [NC]
RewriteRule /Product/Product\.aspx https://www.example/blue-widgets/widget82/? [L,R=302,NC]
RewriteBase /~username/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~username/mysite/index.php [L]
To better explain for others, the order of the rules is important. Since the Wordpress rules change the RewriteBase and change the URL the rules for the .aspx pages will never be hit if they are at the bottom.

How to remove and avoid url directories in htaccess

I'm trying to allow my site to rewrite urls. I have put the following into my .htaccess file in the root directory.
RewriteEngine On
#would be nice to remove member-pages from the URL but no idea how.
#RewriteRule ^members/(.*)/?$ /$1 [NC,R]
#This part works though!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ ./members/$1/ [L]
So far, it takes
mydomain.com/someUserName or mydomain.com/someUserName/ (with trailing slash) and, if it exists, will load the page at mydomain.com/members/someUserName/ without a hitch. This works like a gem.
What I want now (and am trying to do with the first rewrite rule) is to take a mydomain.com/members/someUserName or mydomain.com/members/someUserName/ and have it show up as mydomain.com/someUserName in the url.
How do I do this? Thanks in advance!
If I understand you correctly, You want to redirect domain.com/members/foo to domain.com/foo , You can use the following rule for that:
RewriteEngine On
RewriteCond %{THE_REQUEST} /memebers/([^\s]+) [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ./members/$1 [NC,L]

Rewrite in htaccess trying to do a transparent redirect: rules with [R=301] works but not [L]

I have some rewriteRule in my .htaccess.
I'm trying to do a transparent redirect for all urls starting with _ (like: http://mydomain/_hello to http://mydomain/object/hello). Here's what I tried:
RewriteRule ^_([A-Za-z0-9_-]+)$ object/$1 [L] but it gives me a "Page not found" error.
When I change to [R=301,L] (not [R=301]), it works but the url changes in the browser.
My full .htaccess:
RewriteEngine on
RewriteBase /
RewriteRule ^_([A-Za-z0-9_-]+)$ index.php/object/view_by_alias/$1 [L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I really don't understand what's happening?
Try this:
Add Options -Multiviews above the rules
Change back to [L]
Also, note that you don't need QSA since you're not adding anything to the query string.