Matching sequence of numbers in URL after specific text - regex

I'm trying to match a sequence of numbers that comes after "job=" while ignoring what comes before it and after it like "list.php?job=11534&name"
Below I've posted what I have so far, which seems to have no affect on the URL.
Also, will [L] only stop the rewrite engine if the rule matches or no matter what?
Thanks in advance for your help.
Current Rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)job=([^&]+)
RewriteRule ^([0-9]+)$ /positions/job/$1 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteRule ^([0-9]+)$ will match requests that contain only numbers (/4666), this regular expression should be placed in the RewriteCond instead, %1 will refer to it, and the trailing ? in the rewriterule exclude the query string from the new url (is that what you want?).
L will stop the rewrite engine for the current url only if the rule matches.
RewriteCond %{QUERY_STRING} (?:^|&)job=([0-9]+)
RewriteRule ^.+$ /positions/job/%1? [L]
if you want to redirect the user to the new URL, add R to the rewriterule options ([L,R=301]),
be careful that these directives will redirect any url where the query string matches job=([0-9]+) to the new url, if you wanna to restrict the redirection to certain requests(like /folder/list.php?job=qsd&name=qsd), be sure to include them in the rewrite rule, so the directives would be :
RewriteCond %{QUERY_STRING} (?:^|&)job=([0-9]+)
RewriteRule ^folder/list\.php$ /positions/job/%1? [L]

Related

.htaccess redirect URLs with specific path to 404 and exclude some URLs with the same path as exception

I have an application on Codeigniter(3.1.11). Basically, I want a 404 redirect of URLs which have dashboard in URI. Like these:
dashboard/foo/bar
dashboard/foo-2/bar-2
dashboard/something
...
Also, I want to keep some exceptions in redirect rule so, some of the specific URLs which have dashboard as path URI should be excluded from this redirect. Let's say I want to exclude some URLs like:
dashboard/new-foo/new-bar
dashboard/one-thing/abc
dashboard/another-thing/xyz
I have given a few tries but the exclude rule is not working. It redirects all URLs to 404. This is what I have in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(dashboard/new-foo/new-bar) [NC] # Exclude this url (this condition is not working)
RewriteRule ^(.*)$ dashboard/$1 [R=404,L]
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It can be done in a single rule with negative lookahead conditions like this:
RewriteEngine on
RewriteRule ^dashboard(?!/new-foo/new-bar|/one-thing/abc|/another-thing/xyz) - [R=404,NC,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I'm not expert with htaccess but I am pretty sure you need 2 RewriteCond lines, one to include all the dashboard urls and then the second to exclude the new ones. Each RewriteCond is implicitly joined with an "AND" so if you have many different patterns to exclude and you need a 3rd RewriteCode then you will need to join the 3rd with an "OR" on the 2nd condition.
e.g.
RewriteCond %{REQUEST_URI} ^/dashboard/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/dashboard/new-(.*) [NC]
RewriteRule ^(.*)$ 404-page/$1 [R=404,L]
There are a couple of other things I'd like to mention: 1) Your RewriteRule redirects back to a /dashboard URL so you could possibly end up in a continuous look here. 2) You don't need to turn on Rewrite engine twice, once at the top is enough.
If the rewrite rules in htaccess are getting complicated then perhaps you could use your index.php file to handle it (or some other method within Codeigniter).

get current url and pass part of it as variable

I need to write optimized .htaccess rules. I had written like that and they are working
RewriteEngine on
RewriteRule ^city1-name-in-url/$ products.php?city=city1-name-in-url
RewriteRule ^city2-name-in-url/$ products.php?city=city2-name-in-url
RewriteRule ^city3-name-in-url/$ products.php?city=city3-name-in-url
RewriteRule ^city4-name-in-url/$ products.php?city=city4-name-in-url
And url for these rules are http://www.example.com/global/city1-name-in-url/
I have to write these rules for 800 cities which is making website slow. I want to know if there is anyway to get the part url and use it in RewriteRule
Example like
If url : http://www.example.com/global/any-city-name/
RewriteRule ^any-city-name/$ products.php?city=any-city-name
This is possible? To get the part of url after global and then use it in rewrite rule.
You can use this rule in /global/.htaccess:
RewriteEngine On
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ products.php?city=$1 [L,QSA]
[\w-]+ matches 1 or more of [a-zA-Z0-9_-] characters.

.php ending up in rewritten url after adding trailing slash

I've got an issue when I'm trying to add a trailing slash to non existent files. Here is my rewrite rules
# remove www from url
RewriteCond %{HTTP_HOST} ^www.goautohub.com [NC]
RewriteRule ^(.*)$ http://goautohub.com/$1 [L,R=301]
#rewrite news/article name
RewriteRule ^news/([^/]*)/$ news.php?viewnews=$1 [NC,L]
#remove index from url
RewriteRule ^index\.php/?$ / [L,R=301,NC]
#remove php from url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The only thing left right now I want to do is rewrite this url
/news/mustang-cobra-model-highlights
to
/news/mustang-cobra-model-highlights/
If I use use something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
which I found from Force trailing slash at end of rewritten query string it works but it screws up all my other ones it there is already a trailing slash. What it does it adds
/.php/ to the end.
I figure I need a way to limit that to just the news page but I can't seem to get the rule right.
The followin rewrite rule should work:
RewriteRule ^/news/(.*)/$ /news/$1 [NC,L]

What does RewriteRule ^(.*)/$ ?path=$1 [QSA,L] mean in my .htaccess?

I need to create rewrite in nginx as is done in my .htaccess and there are some lines which I don't completely understand.
DirectoryIndex index.php
RewriteEngine on
RewriteCond % !-f
RewriteRule ^(.*)/$ ?path=$1 [QSA,L]
Can someone explain it to me?
RewriteCond % !-f seems incorrect rule condition and is always evaluating to true.
This rule:
RewriteRule ^(.*)/$ ?path=$1 [QSA,L]
Is matching any URI with trailing slash and internally rewriting to /?path=uri-without-slash
So for ex: an URI /foo/ will be rewritten to /?path=foo
QSA - Query String Append
L = Last rule
Reference: Apache mod_rewrite Introduction
UPDATE: Change that incorrect condition to:
# request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ ?path=$1 [QSA,L]
It means if the request isn't to a file, then rewrite everything before a trailing / into index.php?path= followed by what was previously matched.
It should be the last rule (L) and it should append the query string (QSA), as opposed to discarding it because of the replacement's query string.

ExpressionEngine mod_rewrite Rule to Redirect URLs with Underscores to Dashes

I'm using ExpressionEngine as my CMS and would like to remove underscores from my site's URLs and replace them with dashes.
For example, I've got a URL that is formatted like this:
http://example.com/index.php/menu/friday-lunch
To remove index.php from the URL, I'm using the following mod_rewrite rule:
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Which works, since I can just type in: http://example.com/menu/friday-lunch
On the old site I used underscores instead of hyphens for page URIs, so I wrote a mod_rewrite rule to to redirect URIs with underscores to use dashes.
So friday_lunch becomes friday-lunch using the following RewriteRule:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
This rule works rather well, except that it 301 Redirects to example.com/index.php/menu/friday-lunch instead of example.com/menu/friday-lunch — notice the addition of index.php.
Here's the entire .htaccess I'm currently using:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
</IfModule>
How can I redirect all of my URLs with underscores to the equivalent with dashes?
Bonus: to make matters worse, URLs that lead to /system, must not be rewritten with a hyphen, e.g.: example.com/system/login_in/.
Here's a complete set of RewriteRules that should do what you need:
<IfModule mod_rewrite.c>
# Enable Apache's RewriteEngine
RewriteEngine On
# Ignore Matching Directories
RewriteRule ^(images|themes|system) - [L,NC]
# Replace Underscores with Dashes
RewriteRule ^([^_]*)_([^_]*)_(.*)$ /$1-$2-$3 [R=301,L]
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
# Remove index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
To have your mod_rewrite rules ignore the ExpressionEngine system folder and not replace underscores _ with dashes - use the following:
RewriteRule ^(images|themes|system) - [L,NC]
Dissecting the RewriteRule into plain English:
The - flag instructions Apache to do nothing, and to not rewrite the URI
The L flags means this should be last rule; ignore everything following
The NC flag means no-case (so "System" or "SYSTEM" is also matched)
This "ignore" rule is especially important and you may need to add additional directories to exclude depending on your directory structure.
Otherwise, you may end up with images and other files saved with underscores that get replaced with dashes.
Note: If your URLs contain more than three underscores, you'll need to add another RewriteRule above the existing ones for each Word Separator for URL Titles you want to replace:
RewriteRule ^([^_]*)_([^_]*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4-$5 [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_(.*)_(.*)$ /$1-$2-$3-$4 [R=301,L]
You included 'index.php' in your replacement string.
RewriteRule ^(.*)$ index.php/$1 -> RewriteRule ^(.*)$ $1