Rewrite only root without redirect not working - apache

I have a problem that I can't get to rewrite only root to file. For example example.com to example.com/landing-page/mypage.html but keep all other addresses like example.com/something/ intact.
Currently for test purposes I have managed to redirect /test/ and my .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/test/.*$
RewriteRule . /wp-content/themes/mytheme/landing_page.html [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But now if I try to redirect only root and replace RewriteCond with
RewriteCond %{REQUEST_URI} ^/$
it doesn't work and it loads my Wordpress site.
UPDATE:
I tested on another server using just simple rule RewriteRule ^/?$ /mypage/landing-page.html [L] also keeping WordPress lines and it works there, but the same lines do not work on this server. So now the question arises what server settings might be preventing this simple rule.

Looks like you forgot to turn on the RewriteEngine in your second example .
RewriteEngine On
RewriteRule ^$ /landing-page/mypage.html [NC,L]

Related

Rewrite rule for opening a different page

I'm stuck with some .htaccess rules.
I've got the page /module/slug. I want, when I type slug to see content of /module/slug/ but not redirect to this page. Is it possible? I've tried this rule, but had no luck:
RewriteRule ^slug/$ /module/slug/ [L]
My .htaccess content:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^calculator/$ /sample-page/ [R=301,L]
</IfModule>
# END WordPress
That happens because you don't tell Apache to redirect (R=301).
Add the redirect flag R, and it should work. Also make sure that:
You have set the RewriteBase correctly.
There are not any other rules earlier in the file that match the same URL.
.
RewriteEngine On
RewriteBase /
RewriteRule ^slug/$ /module/slug/ [R=301,L]

Apache .htaccess RewriteRule not including subdirectory when redirecting non-www to www url

I've setup a rewrite rule in order to direct domain.com traffic to www.domain.com using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
From my limited understanding, the $1 in the RewriteRule should match anything after domain.com/ and then be placed at the end of my rewritten url www.domain.com/.
For example domain.com/abc should become www.domain.com/abc.
However this is not working. If I browse to domain.com/abc then this path isn't rewritten at all and I just get domain.com/abc without the www.
I've done lots of reading just to figure out that the $1 should be taking care of this, from my understanding.
Can anyone explain why it isn't working as I suspect it should be? Thanks.
It turns out that because I was using Wordpress AND W3 Cache plugin that I was simply putting the code in the wrong place in my .htaccess file.
The code should be placed within the # BEGIN Wordpress codeblock and before the existing rules that Wordpress already has in place.
The existing code should look something like this:
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END Wordpress
I added my original code to this as follows:
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END Wordpress
This fixes the issue I was describing above. Thanks to the commenters for sparking my thought process.

PHP Rewrite SEO friendly URL - Inside folder

This a php mod rewrite related question
Background info :
I am using wordpress site in my root ( example.com)
I have a folder created under that called 'search-jobs', which has all the php code ( example.com/search-jobs) I am not using wordpress for anything on this search page, but wanted to ensure you have that info and if it impacts.
I am trying to rewrite the below URL :
http://example.com/search-jobs/?searchText=FACEBOOK+INC.&searchCity=Enter+US+City+or+Zipcode&searchYear=14&action=search&searchJobTitle=Enter+Job+Title+%2F+Role+Name
I have the below mod rewrite written in the .htaccess file, which is placed in the root folder, where wordpress is installed.
# BEGIN search Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^company/([0-9a-zA-Z_-\s]+)/([0-9a-zA-Z_-\s]+)/([0-9]+)/([0-9a-zA-Z_-\s]+)/([0-9a-zA-Z_-\s]+)$ /search-jobs/?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L]
</IfModule>
# END search Rewrite rules
Also, four of the variables typically have spaces as they are search strings. Not sure, if that is causing issues.
Below is the full Wordpress rewrite rule that excludes the folder
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(search-jobs|search-jobs/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I saw some wordpress rewrite rules in the .htaccess file, is that messing up my rewrite rules or am I doing something wrong ? I am doing this for the first time. I have tried for couple of days on this reading many other, badly stuck. Any help would be great.
Have root WP .htaccess like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(index\.php|search-jobs(/.*)?)$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Then inside /search-jobs/.htaccess have code like this:
RewriteEngine On
RewriteBase /search-jobs/
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=([^\s&]+)&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ ?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L,QSA]

Remove specific slug in with htaccess

I have a Wordpress site web in localhost with XAMPP. Now I have a URL like this:
http://localhost:82/subdirectory/images.html/nggallery/tags/[tagname]
Now I want this:
http:// localhost:82/subdirectory/images/nggallery/[tagname]
To remove tags, I've tried many codes, like this:
RewriteRule ^tags/(.+)/$ /$1 [R=301,L]
but doesn't work.
Help me, please.
I'm using WordPress, my htaccess file looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</IfModule>
# END WordPress
Before answering, a better idea than using htaccess for this is to use Wordpress' rewrite modules.
Anyway, you need to put your specific rules before Wordpress' main rule.
You can replace your current code by this one in your htaccess (which has to be in your subdirectory folder)
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /subdirectory/
# redirect /.../tags/XXX to /.../XXX
RewriteCond %{THE_REQUEST} /images\.html/nggallery/tags/([^\s\?]+)\s [NC]
RewriteRule ^ images/nggallery/%1? [R=301,L]
# internally rewrite /.../XXX to /.../tags/XXX
RewriteRule ^images/nggallery/(.+)$ images.html/nggallery/tags/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
# END WordPress
try
http:// localhost:82/subdirectory/images/nggallery/[tagname]
RewriteRule ^nggallery/tags/(.*)$ http://localhost:82/subdirectory/images/nggallery/$1

.htaccess parse url works on localhost but gets redirect loop on VPS host

I'm trying to parse urls with .htaccess and pull our either 1 or 2 parameters to load content based on the url.
If only the first url param (site.com/param1) is present then that's fine and should route to index.php?c=param but could also be (site.com/param1/param2) and get routed to index.php?c=param1&p=param2
It works perfectly on MAMP PRO running on Mavericks but when I clone the repo onto our VPS I get a redirect loop. There is an admin area for the site so I need to ignore the any url that follows the pattern
site.com/admin
The "parsable" urls follow this pattern
site.com/param1
or
site.com/param1/param2
The .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
## SEO REWRITES ###
RewriteRule ^(admin) - [L]
RewriteRule ^/?([a-zA-Z0-9_]+)/?$ /index.php?page=$1
RewriteRule ^/?([a-zA-Z0-9_]+)/([0-9]+)/?$ /index.php?page=$1&e=$2
## URL PARSE ##
RewriteRule ^([^/]+)$ /index.php?c=$1
RewriteRule ^([^/]+)/([^/]+)$ /index.php?c=$1&p=$2
</IfModule>
I'm lost but I feel like I'm missing something simple here. The most confusing part is that it has worked since development began. Anyone have any ideas as to why the redirect loop when running on an vps with apache but not on localhost or more importantly how to get it parsing on the live server.
Im now using this which works except when the trailing slash is missing from the url, then it breaks, Ive tried dif variations to let it match without a trailing slash but i just get 404's or internal server errors:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(admin) - [L]
RewriteRule ^/?([a-zA-Z0-9_]+)/?$ /index.php?page=$1
RewriteRule ^/?([a-zA-Z0-9_]+)/([0-9]+)/?$ /index.php?page=$1&e=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/([^/]+)$ /index.php?c=$1&p=$2 [L,QSA]
RewriteRule ^/?([^/]+)/$ /index.php?c=$1&p=index [L,QSA]
</IfModule>
Have it this way:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
## SEO REWRITES ###
RewriteRule ^(admin) - [L]
RewriteRule ^/?(\w+)/([0-9]+)/?$ /index.php?page=$1&e=$2 [L,QSA]
RewriteRule ^/?(\w+)/?$ /index.php?page=$1 [L,QSA]
## URL PARSE ##
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?c=$1&p=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /index.php?c=$1 [L,QSA]
</IfModule>
The problem is that
RewriteRule ^([^/]+)$ /index.php?c=$1
matches
index.php?c=blah
You need to add a rule so that any valid paths are served without modification.
Try adding
RewriteCond %{REQUEST_FILENAME} !-f
just after RewriteEngine on