Redirects based on file folder structure - apache

We have an website whose url structure is undergoing major changes. As there are lots of pages, I am trying to find a method that redirects based on the requests file structure AND the lack of a file extension.
The webpages are primarily one of two version:
mydomain.com/some-page-title
mydomain.com/multiple/folder/levels/some-other-page
The end goal is:
mydomain.com/some-page-title.htm
mydomain.com/newfolder/some-other-page.htm
I know I can assign a condition to point to a specific redirect for the second example, as in the upper code block. And I can do a simple redirect for the first example. But disappointingly both rewrites effect the urls with multiple/file/folders structure and I end up with two redirects, one with the new folder and one with the original folders.
RewriteCond %{REQUEST_URI} !\.[a-zA-Z0-9]{3,4}
RewriteCond %{DOCUMENT_ROOT}/multiple/$1 -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^multiple/folder/levels/(.*)$ /learn/$1.htm [R=301,NC,L]
RewriteCond %{REQUEST_URI} !\.[a-zA-Z0-9]{3,4}
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1.htm [R=301,NC,L]
Is there RewriteCond that says CAN NOT contain a specific file structure? Thanks

You may try these rules that won't affect each other:
RewriteEngine On
RewriteRule (?:/|\.[a-z0-9]{3,4})$ - [L,NC]
RewriteRule ^multiple/folder/levels/(.+)$ /learn/$1.htm [R=301,NC,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/learn/ [NC]
RewriteRule ^(.+?)/?$ $1.htm [R=301,NE,L]
RewriteCond %{REQUEST_URI} !^/learn/ [NC] will prevent second redirect rule to execute after first redirect.
Make sure to clear browser cache or use a new browser for testing this change.

Related

htaccess to hide subdirectory without changing base root, and with exception of another subdirectory

I have a website https://example.com
which has 2 subdirectories, https://example.com/admin and https://example.com/store.
I want to hide /store from URL so my requests would look like:
https://example.com/shop.php?id=someid
But Also, I want https://example.com/admin/index.php to work.
I found some answers that achieve both 1 and 2 but they change base root so all my css,js, images don't load
So far I have:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^store/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^store/ store%{REQUEST_URI} [L]
This achieves 1 but not 2.
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^store/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^store/ store%{REQUEST_URI} [L]
In the second rule... exclude the /admin subdirectory (as well as /store) in the RewriteRule pattern. And add a condition to exclude requests that contain a file extension (ie. .css, .js, .png, etc.). For example:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteRule !^(store|admin)($|/) store%{REQUEST_URI} [L]
Alternatively (although marginally less efficient), exclude any request that already maps to a physical file:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(store|admin)($|/) store%{REQUEST_URI} [L]
This rule assumes you have another .htaccess file in the /store subdirectory that also uses the rewrite engine. (But if that is the case then the first rule isn't actually doing anything.)
Unless you are hosting multiple domains/sites then you don't need the first condition that checks the requested Host.

how to remove query string from url using htaccess

Once my friend help me to solve this issue, I have written code like that. Where example.com/blog-detail?slug=slugvalue it goes to blog-detail.php and this url example.com/project?slug=test goes to project.php.
RewriteEngine ON
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ blog-detail.php?slug=$1 [QSA,L]
I think it's work for only blog-detail page now i face the issue with this url
https://www.example.com/project?slug=test, it redirect me to 404 page, could you help me here.
With your shown samples, attempts; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
I have added 2 solutions here, so either use 1st rules set OR use 2nd rules set ONLY ONE at a time please.
1st solution: With 2 different rules set for each php file please try following rules.
RewriteEngine ON
###Rules for blog-detail.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(blog-detail)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ blog-detail.php?slug=$1 [QSA,L]
###Rules for project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(project)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ project.php?slug=$1 [QSA,L]
2nd solution: A Generic solution with capturing groups to deal with both kind of php files together.
RewriteEngine ON
###Rules for blog-detail.php OR project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ /$1.php?slug=$2 [QSA,L]
NOTE1: Please keep both your php files(project.php and blog-detail.php) should be kept along with your htaccess rules file.
NOTE2: For JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

RewriteRule for several parameters

I'm currently working on a project powered by a home-made CMS and I'm experiencing some issues with URL rewriting.
Here's the thing: all the website is centralized around the index.php located in the main directory. Depending on what he gets thought the URL, the index.php displays the right page (the pages are included from a inc/pages/ folder)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [NC]
For a single parameter, it works great. http://demo.com/subscribe/ or demo.com/subscribe does transmit a $_GET['page'] to the index.
For some pages, I do need a second parameter. So it's not required for each single pages. Per example, http://demo.com/edit/I-love-Stackoverflow should transmit a $_GET['snd_param')
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.php?page=$1&snd_param=$2 [NC]
I tried this but this isn't working well. First, if the second parameter is not mentioned (demo.com/edit) it's not working. The index doesn't receive the right $_GET['page']. Secondly, when the second parameter is mentionned, it "works" but apache believes this is a directory. My index page is then located in the fictive "I-love-Stackoverflow" folder and loading the CSS, images and javascript fails.
I hope I explained my issue pretty clearly ! Thanks in advance for helping me
You should treat the rules separately. All Conditions preceding rules only apply to a single rule, so basically the second RewriteRule is not executed at all.
You can use something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&snd_param=$2 [L]
My index page is then located in the fictive "I-love-Stackoverflow"
folder and loading the CSS, images and javascript fails.
You are probably load your assets using relative paths, so the browser only knows for the unmodified url ( http://demo.com/edit/I-love-Stackoverflow ) in your case, and the wrong urls are created when browser load the assets. If you load resources with absolute paths instead of relative, you will be okay.

apache redirect to naked (non-www) domain messes up pages handler

all the urls in my website actually go through a PHP page that handles them by the page GET parameter (i.e. domain.com/sub/test is actually domain.com/page_handler.php?page=sub/test).
files or directories that exist don't go through the handler.
I've been trying to 301 redirect all www.domain.com requests to domain.com for improving SEO etc.
the problem is that this doesn't seem to work, no matter what rule I use and where I put it. this is the .htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#THIS IS THE DISCUSSED RULE:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule (.*[^\/])$ page_handler.php?page=$1 [QSA,NC,L]
RewriteRule ^/?$ page_handler.php?page= [L,QSA]
DirectoryIndex page_handler.php?page=
When I put the rule in the current line, it works ok with pages that are supposed to go through the handler BUT it makes existing resources go through it as well (e.g. domain.com/page_handler.php?page=js/script.js) which is not good.
When I put it after the other rules it redirects www.domain.com/something to domain.com/?page=something.
So, the question is: how to redirect urls that begin with "www." to the naked (non-www) domains without affecting the other rules?
Thank you!
The problem with your code is that you are applying the first two conditions only to the non-www rule. Conditions can only be tested for the rule that immediately follows them.
So, you'll need to move those down, and clean up a bit:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /page_handler.php?page=$1 [QSA,NC,L]
If this causes issues for you, then you may want to change the way your page is detected, by using the REQUEST_URI instead of a $_GET['page']. If you want to do this (which is actually a better method), the last rule can be changed to the following:
RewriteRule ^ /page_handler.php [QSA,NC,L]

flexibility in .htaccess for mod_rewrite rules

i would like to redirect my old php files to new seo friendly ones:
user.php?user=$var1&task=$var2 -> url/$var1/$var2
There are 2 problems. $var2 is not set every time, so i do not know how to deal that and the querystring is always added at the end.
I use the following redirect rule for testing (without $var2)
RewriteCond %{THE_REQUEST} ^(.+)user\.php(.+)$
RewriteCond %{QUERY_STRING} user=([^/]+)$
RewriteRule ^(.+)\.php$ %1 [R]
I get this:
url/$var1/?user=$var1
Second problem is the rewrite rule so that url/$var1 -> user.php?user=$var1
Without it i get a server error.
In the moment i tried this static one for testing, but this is not the only rule so that the Condition is wrong here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ user.php?user=$1 [L]
How i get the correct results and more flexibility with the variables?
mod_rewrite is not my world in the moment, so i hope you can bring light in the dark.
Thx ruven
1) The way to prevent the query string at the end there is to add a ? at the end of the URL you rewrite to.
RewriteCond %{THE_REQUEST} ^GET\ /user\.php
RewriteCond %{QUERY_STRING} user=([^&=]+)$
RewriteRule ^user\.php %1? [R]
And in case both var1 and var2 are set it would be
RewriteCond %{THE_REQUEST} ^GET\ /user\.php
RewriteCond %{QUERY_STRING} user=([^&=]+)$
RewriteCond %{QUERY_STRING} task=([^&=]+)$
RewriteRule ^user\.php %1/%2? [R]
Combine these (the second one first) and it should redirect as needed
2) Since this is a kind of 'catch all' URL you should put this as the last option in your .htaccess and redirect everything that is not a file or a directory to user.php and then let user.php figure out if the user exists, and if not respond with HTTP 404.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ user.php?user=$1 [L]
(I've removed the / at the end as it's not a good idea to have two URLs for the exact same content).