PHP Rewrite SEO friendly URL - Inside folder - apache

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]

Related

.htaccess 301 redirect with exclusion does not work

I try to use a simple 301 redirect
from domain1.com/folder/ to domain2.com/
but excluding domain1.com/folder/subfolder
I use the following code in .htaccess:
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
but it simply redirects all the requests, including the requests to subfolder.
Please, help to fix the line to make it work as described. Thank you!
here is the complete code of .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
Try it like this using mod_rewrite instead:
(NB: This assumes the .htaccess file is located in the document root.)
# /.htaccess
# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule ^folder/(.*) https://domain2.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
It is important that the redirect goes before the rewrite to your front-controller.
You will need to ensure your browser cache is cleared before testing and test with a 302 (temporary) redirect to avoid potential caching issues.
UPDATE:
Yes, /folder has it's own .htaccess (this is the file I am working at all this time). Yes, /folder is where Wordpress is installed.
In that case you would need to change the above redirect to read as follows (it won't do anything otherwise):
# /folder/.htaccess
# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule (.*) https://domain2.com/$1 [R=301,L]
Basically, you need to remove folder/ from the start of the regex that matches the URL-path. The URL-path that the RewriteRule pattern matches against is relative to the directory that contains the .htaccess file.
The addition of the check against the REDIRECT_STATUS env var is to ensure that rewritten requests to the WP front-controller (when subfolder is requested) are not redirected.
You can also "simplify" the WordPress directives that follow (although if these are enclosed in # BEGIN WordPress / # END WordPress comment markers then you should leave the directives as they are since they are maintained by WordPress). For example:
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
The RewriteBase directive is not required. And neither is the <IfModule> wrapper. (But as I said above, only change this if you are hand-coding the .htaccess and not letting WordPress maintain it.)

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]

Rewrite only root without redirect not working

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]

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

Struggling with Apache .htaccess RewriteRule

I am currently redoing one of my sites in Wordpress. This installation is currently sitting in wwww.mydomain.com/wordpress while I test it.
On my main site requests come in as so:
http://www.mydomain.com/index/newsid/16589/some-text-here-that-is-irrelevant
The .htaccess at http://www.mydomain.com/.htaccess currently looks like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule index/newsid/(.*) index.php?newsid=$1
RewriteEngine On
My Wordpress .htaccess currently sitting at www.mydomain.com/wordpress/.htaccess is as follows
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
When I migrate my site all links from other sites are going to break, I have incorporated all the articles elsewhere in an archive in a forum. I need to effectively do two things, make my .htaccess work on the root of my site for wordpress after moving it AND redirect any newsid requests elsewhere.
Essentially I want to turn this:
http://www.mydomain.com/index/newsid/16589/some-text-here-that-is-irrelevant
Into:
http://www.mydomain.com/forums/index.php?showtopic=16589
They primary id here is 16589 which is a variable and can change.
Any help would be appreciated, I can do simple .htaccess files but this is beyond me.
You'll need to add some rules before your wordpress rules after you've moved your wordpress installation to your document root. You'd need to do something like this in the htaccess file in your document root:
Options +FollowSymLinks
RewriteEngine on
# redirect your newsid
RewriteRule ^index/newsid/([0-9]+)/ /forums/index.php?showtopic=$1 [L,QSA,R]
# here are the wordpress rules
# 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
If you want 301 redirects instead, replace the [L,QSA,R] with [L,QSA,R=301].