.htaccess rewrite rule for URL with round bracket - apache

The company I work for just recently sent out a mass email and got the URL for the footer wrong. The correct URL is /email/footer.png, but the email has the URL /email/footer.png) (note the trailing bracket).
I have tried the following .htaccess RewriteRule with no luck. RewriteEngine is on, mod_rewrite.c is loaded, and rewrites are working for a number of other RewriteRules:
RewriteRule ^/email/footer\.png\)$ /email/footer.png
Does anyone know the fix for this?

You need to remove the leading slash from your uri pattern
RewriteEngine on
RewriteRule ^email/footer\.png\)$ /email/footer.png [L,R]

Related

Trailing slash mod_rewrite rule for certain subfolders

I am trying to get a trailing slash mod_rewrite rule to work for only certain subfolders.
I have article URLs like ////. For example an article URL may be /news/role-playing-games/new-roleplaying-game-released/. Sometimes, for whatever reason, people try the URLs without the trailing slash and they get a 404. I would like instead for apache to add a trailing slash to only the URLs that match certain subfolders.
I tried this rule, but it does not work.
RewriteCond %{REQUEST_URI} ^/(news|reviews|tutorials|guides)/(.*)$
RewriteRule ^(.*)$ /$1/ [L,R=301]
Can anyone tell me what I am doing wrong?
Thanks in advance.
Try
RewriteRule ^((news|reviews|tutorials|guides)/[^/]+/[^/]+)$ $1/ [L,R=301]
I assumed that the url structure you gave is the only one.

Redirect from PHP file to just query string

I want to use mod rewrite to redirect the following dynamic URL with h query string:
https://eu1.domain.com/~username/test.php?h=String_112016
To the following URL without the test.php part:
https://eu1.domain.com/~username/String_112016
I tried the following in my htaccess file but it's getting 404:
RewriteEngine On
RewriteRule ^\~username/([^/]*)$ /~username/test.php?h=$1 [L]
I don't have any other rule in the htaccess file.
From the code you wrote, I'm assuming what you want is not actually a redirect. Correct me if I'm wrong, but what you want is that when a user requests /~username/String_112016, you want the test.php page to be rendered and passed "String_112016" as a parameter labeled h.
You're really really close... Try this:
RewriteEngine On
RewriteRule ^/?~username/([^/]*)$ /~username/test.php?h=$1 [L]
The first matched character in the Rewrite Rule should be a forward slash, not a backslash. This forward slash is practically always present in requests, but I think it's best practice to treat it as optional, because technically you could get a (malformed) request that does not have one. The question mark after the /, makes the / optional.
RewriteEngine On
RewriteBase /
RewriteRule ^~username/test.php\?h=(.*)$ /~username/$1 [L]

Replacing slashes with dashes in URL Rewriting

How do I accomplish URL rewriting for the following with mod_rewrite in Apache?
Pretty Link : www.mysite.com/pages/category/page/
Actual File : www.mysite.com/html/category-page.html
I only want to rewrite URLs if they contain the domain and the pages directory. In all other cases I want the server to work normally.
I've come up with this but would like to know how to replace slashes with dashes:
RewriteEngine On
#Look for the word "pages" followed by a slash, and then the article title
RewriteRule ^pages/(.+)$ html/$1.html [L]
See if the following rule set works:
RewriteEngine On
RewriteRule ^pages/(.+?)/?$ html/$1.html [N]
RewriteRule ^html/([^/]+)/(.*)$ html/$1-$2 [N]

Simple RewriteRule for first directory

This is pretty basic but I can't find a solution that works. I just need to redirect any URLs from an old directory to a new URL.
Example:
/search/whatever
to
/jobs/search
I don't need to map the whatever, I want all traffic to a URL that begins /search to get redirected (301).
Using this:
RewriteRule /search /jobs [R=301,L]
Works but I have a URL within /jobs that also now gets redirected:
/jobs/search
And that's wrong - it needs to match the start of the URL. So I tried this:
RewriteRule ^/search /jobs [R=301,L]
But that doesn't redirect at all, so I'm stuck.
Another example would be this:
RewriteRule /careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
This creates a loop as careers-at-pure is in the old and new URLs, but the following doesn't get matched and redirected:
RewriteRule ^/careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
Any suggestions?
Thanks
The leading slash is removed from the URI when they're being put through rewrite rules in htaccess files. You need to remove the leading slash from the regex, or at least make it optional:
RewriteRule ^/?search /jobs [R=301,L]

RewriteRule, url wrongly displayed

I am new to mod_rewrite and I have this problem:
I have a working redirect with mod_rewrite, my .htaccess:
RewriteEngine on
RewriteRule ^microsite/([^/\.]+)/$ micrositecontroller.php?name=$1 [L]
micrositecontroller.php only echoes a text
On the browser when I enter my URL:
localhost/project/microsite/test/
I am redirected to where wanted but when I enter:
localhost/project/microsite/test
It still redirects to where wanted but the URL becomes like this:
localhost/project/microsite/test/?name=test
Now what I want is that trailing "/?name=test" not to show up.
I tried different combinations of the regex but to no avail and I have no idea if it is normal of not. Any idea?
You have a slash in your regex, so your regex handles slash... just remove it:
RewriteEngine on
RewriteRule ^microsite/([^/\.]+)$ micrositecontroller.php?name=$1 [L]
What you want is:
RewriteEngine on
RewriteRule ^microsite/([^/\.]+)/?$ micrositecontroller.php?name=$1 [L]
# Note the "?"-------------------^
To accommodate both ending with a slash and no slash. The problem with leaving the slash on (or off) is apache forces a browser redirect, thus making the ?name=test show up in the browser's location bar.
This is because mod_dir and the DirectorySlash on directive is interferring. The DirectorySlash directive tells apache to redirect the browser when it accesses what looks to be a directory (in your case localhost/project/microsite/test to the smae URI except with a trailing slash.