Replacing slashes with dashes in URL Rewriting - apache

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]

Related

.htaccess rewrite rule for URL with round bracket

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]

Redirect directory with period to query strings in htaccess

I want to have personalized urls, where a person receives a mailer, that has their purl on it, and redirect them to a landing page that receives their name via query string
The url would be like
mywebsite.com/phx/joe.smith
I would like to redirect this traffic to
mywebsite.com/youngstown/index.php?first=joe&last=smith
This should be a 301 redirect, case insensitive. I'm able to do it if the directory was /phx/firstname/lastnaname, but I need it with the dot between first and last rather than a directory.
What I have so far is
RewriteEngine on
RewriteBase /
RewriteRule ^phx\/([^\/]+)\/([^\/]+)\/? youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
RewriteRule ^phx\/([^\/]+)\/? youngstown/index.php?FirstName=$1 [NC,R=301,L]
Any help would be much appreciated!
First, you don't need to escape slashes /. Apart from that, you're almost there. The main difference is \. vs /, e.g.
RewriteRule ^phx/(.+?)\.(.+)/?$ youngstown/index.php?first=$1&last=$2 [R,NC,L]
A complete solution could be:
RewriteEngine on
RewriteRule ^phx/([^./]+)\.([^./]+)/?$ /youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
RewriteRule ^phx/([^./]+)/?$ /youngstown/index.php?FirstName=$1 [NC,R=301,L]
As Olaf says, no need to escape slashes. I am matching here on "not a dot or slash". Also adding $ to delimit the end of the match and removing RewriteBase which is not needed.
Alternative Solution
Alternatively you could use a single rule which would set an empty param for LastName when not present:
RewriteEngine on
RewriteRule ^phx/([^./]+)(?:\.([^./]+))?/?$ /youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]

htaccess redirect of URL's containing query

I have one URL, domain.com/fun/page/ which i want to redirect to itself when query string is appended to it (domain.com/fun/page/?xyz).
Server is LiteSpeed and no matter what (I've tried numerous rules found here and elsewhere on the web), I don't get what I want. Seems I'm just not skilled in writing matching regex.
put this code in your /fun/.htaccess file:
RewriteEngine On
RewriteBase /fun/
RewriteCond %{QUERY_STRING} .+
RewriteRule ^page/?$ %{REQUEST_URI}? [NC,R=302,L]
Trailing ? will strip off any existing query string.

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.

Trouble with advanced .htaccess redirect

I'm migrating a custom coded blog over to Wordpress, and have to set up a redirect that will handle all of the blog posts.
I need to redirect from this:
/oldblogdirectory/Old_Blog_Posts_Looked_Like_This.htm
to:
/newblogdirectory/new-blog-posts-look-like-this/
Any ideas on the regex for a redirect like this?
Gumbo's approach is certainly the way to do it. I made two test directories:
oldblogdir/archives/blog_posts_look_like_this.htm
newblogdir/archives/blog-posts-look-like-this
And the following RewriteRules redirect successfully. They are only slightly changed to Gumbo's proposal:
RewriteEngine on
RewriteBase /
RewriteRule ^(oldblogdir/archives/[^_]*)_(.*) $1-$2 [N]
RewriteRule ^oldblogdir/archives/(.*?)\.htm$ newblogdir/archives/$1 [R,NC,L]
Note that the [N] causes the .htaccess file to be re-evaluated until the RegEx no longer matches. Therefore you should put it at the very top of the file.
Try this mod_rewrite rules:
RewriteEngine on
RewriteRule ^(oldblogdirectory/[^_]*)_([^_]*)_(.*) /$1-$2-$3 [N]
RewriteRule ^(oldblogdirectory/[^_]*)_(.*) /$1-$2
RewriteRule ^oldblogdirectory/(.+)\.htm$ /newdirectory/$1/ [L,R=301]
But for the uppercase to lowercase conversion you’ll either need a mapping like the internal tolower function or you use PHP for both.