RewriteMap to clean up .htaccess file - .htaccess

Hi I'm not that great at regex and have been tasked with cleaning up a line by line .htaccess file. I've read that RewriteMap is a good alternative to avoid a messy .htaccess file, but I've really got no idea where to start. Most guides seem to require prerequisite knowledge that I do not have. Can anyone help me?

I use ISAPI Rewrite III for Windows IIS, so it may not be exactly the same as php, but if not at least its very close. This is what I use:
RewriteEngine on
RewriteBase /
RewriteMap artMap txt:d:/inetpub/pathToMapDirectory/artMap.txt [NC]
RewriteRule ^Frequency-Machine/(.*) ${artMap:$1} [NC]
"Frequency-Machine" can be any text string that you want to use to trigger the rewrite map operation. I use a different string for each site that I do.
Here is a sample of the map file:
The-Analog-vs.-Digital-Frequency-Debate content1.asp?id=47&catID=1
The-GB4000-Frequency-Generator content1.asp?id=12&catID=1
Video-Tutorial-Flash content1.asp?id=58&catID=1
GB400-Frequently-Asked-Questions content1.asp?id=49&catID=3
Hope that helps.

Related

htaccess from old forum url to new one

until now I have always used htaccess to rewrite URLS in order to have non-SEF url to SEF urls.
Today I am facing a new challenge that honestly, beeing non confident in regular expression, I really don't know how to achieve.
I have a situation where a forum on a website of mine has been update in the following form:
previous link: www.domain.com/forum3/topic/name-of-topic/post/7548
new link: forum.domain.com/Topic-name-of-topic/
How do I intercept /post/37764 string and tell htaccess to not consider it?
And how to instruct the server to build that kind of url instead of the provious. I am very confused about it.
Any suggestion? Thank you very much. Is there any resource that I can read to help me better understand the case?
Thanks again.
EDIT
Florian answer is correct. I just added few mods to fit it better.
RewriteEngine On
#RewriteBase /
RewriteRule ^forum3/topic/([^/]+)/post/[0-9]+$ http://forum.domanin.com/Topic-$1/ [L,R=301]
RewriteRule ^forum3/topic/([^/]+)-[0-9]+$ http://forum.domanin.com/Topic-$1/ [L,R=301]
You can try this code :
RewriteEngine On
RewriteBase /
RewriteRule ^forum3/topic/([^/]+)/post/[0-9]+$ /Topic-$1/ [L,R=301]
/([^/]+)/ means that we want to catch a string containing one or more characters except / preceded and followed by a /.
This link might help you to test your .htaccess files :
Test your apache htaccess files online

URL Rewrite best practice & how to set it up with htaccess

I have a small personal project I'm working on, and before I get too deep into it, I'd like to get some opinions here on the best practices regarding the set up and URL scheme... and how I might set up the .htaccess rewrites for the following. My questions are:
Is the below set up the best way to manage user information in a small user profile setup?
I have worked out how to get the subdomain working for the rewrite username.domain.com with wildcard subdomains and httpd.conf file set up... but I am stuck on the rest of the scheme seen below. Basically, a user profile will always result in username.domain.com and then appended with the various pages within their account (photos, videos, notes etc). How would I set up the .htaccess rewrites to accommodate this? I really appreciate any advice here. I have done a ton of research here on stackoverflow, and on other sites, but I can't find a decent explanation to achieve this.
Thanks for any help.
www.domain.com/profile.php?u=username --> username.domain.com
www.domain.com/photos.php?u=username --> username.domain.com/photos
www.domain.com/photos.php?u=username&a=album --> username.domain.com/photos/album
www.domain.com/photos.php?u=username&a=album1&p=photoid --> username.domain.com/photos/album1/photoid
www.domain.com/settings.php?u=username --> username.domain.com/settings
etc
Your proposed setup looks fine to me. Here are some rules for .htaccess (make sure you have mod_rewrite enabled and AllowOverride All set in your httpd.conf):
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ http://www.domain.com/profile.php?u=%1 [L,QSA]
RewriteRule ^photos/?$ http://www.domain.com/photos.php?u=%1
RewriteRule ^photos/([^/]+)/?$ http://www.domain.com/photos.php?u=%1&a=$1
RewriteRule ^photos/([^/]+)/([^/]+)/?$ http://www.domain.com/photos.php?u=%1&a=$1&p=$2
RewriteRule ^settings/?$ http://www.domain.com/settings.php?u=%1
Wich server do you use? And first of all use rewrites in the config file of your server, not htaccess, htaccess slows the server down.
Edit: Iam not shure about the speed, but as far as i remember htaccess slows down apache, i dont know about how much. Iam sry you have to google that :)

.htaccess rewrite url with parameter

I want to change my url with only the parameter. As I am passing only one parameter, so I want the sub url should be changed with my parameter name. I want change the url as the following type-
From:
http://www.xyz.com/cat.php?slag=season
to
http://www.xyz.com/season
Can anyone help me to do it. I don't know how to do it. Thanks
Options +FollowSymLinks
RewriteEngine On
RewriteBase /path/to/your/directory
RewriteRule ^(.*)cat/(.*)$ cat\.php?slag=$2&%{QUERY_STRING} [L]
put the above in your .htaccess file.. and that would do the magic..
Note :
RewriteBase /path/to/your/directory
(use this line only if your application is in any of subfolder the folder)
I suggest you obtain a copy of the .htaccess that WordPress uses. It's a quite powerful starting point that allows you to handle routing internally from within your Application - which you might feel more comfortable with, as it seems to me.

removing file extension with htaccess failing

i'm using an htaccess script trying to remove the .php testing the .htaccess on a testing server it runs fine, but on the live server that is a different host it trys rewriting the file based on the absolute path and the rewrite fails
here is the htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
this is taking a url like this www.example.com/services
and trying to point it to /n/c/example.com/public/service.php
I know the {REQUEST_FILENAME} is suppose to be pulling the full local system path, but i don't understand why it's not finding the file. i know very little about htaccess and mod_rewriting so i'm not really sure what I should try to make it base everything off of just the url path, or if there is a better solution. I'm really open to suggestions.
Any help would be greatly appreciated.
Thanks
Use RewriteRule .* %{REQUEST_URI}.php [L]
It is hard to tell why your rule did not worked for you by having so little info about your Apache setup and any other rewrite rules that you may have.
Quite possible that the [L] flag did the trick for you -- you may have other rewrite rules that were rewriting this URL further, producing incorrect result in the end. I don't think that %{REQUEST_URI} did such a big job on its own, unless you have some symbolic links / aliases or even some transparent proxy in use which could make a difference.
Keep in mind, that the rules you have shown in your question cannot generate this sort of URL to be visible in browser's address bar (example.com//service.php/) -- it has to be a redirect (3xx code) involved .. which suggests that you have other rules somewhere.
Most likely it is a combination of your Apache specific settings & combined rewrite rules logic (where the L flag can make a big difference depending on those other rules).
The only way to give more precise answer will be enabling rewrite debugging and analyzing how rewrite was executed and what was involved.
Have you enabled mod_rewrite on the other server? AddModule mod_rewrite, I think.
Also - more likely - have you enabled .htaccess? You would need to have
AllowOverride All
or
AllowOverride FileInfo
for that.
These directives will need to go in the apache config files (usually /etc/httpd/conf/httpd.conf or one of the files in /etc/httpd/conf.d), and you will need to restart apache to get them to take effect.

.htaccess RewriteRule help

I've been trying to create a rule.
If anyone could help, I would be extremely grateful.
Request: domain.com/c/wb.php?p=rs/rs/1tb/25n/ru/rs
Rewrite to: domain.com/c/wb/rs/rs/1tb/25n/ru/rs
Thanks in Advance
I think you have this a bit backwards. The idea behind URL rewriting is that you take a nice neat URL like this (what the user sees):
http://domain.com/c/wb/rs/rs/1tb/25n/ru/rs
and rewrite it behind the scenes into an uglier but PHP etc. friendlier URL like this (what the server processes):
http://domain.com/c/wb.php?p=rs/rs/1tb/25n/ru/rs
To do that, use this:
RewriteEngine On
RewriteRule ^/c/wb/(.*) http://domain.com/c/wb.php?p=$1 [L, NS]
It should look something like this
RewriteRule ^(c/wb)\.php\?p=(rs/rs/1tb/25n/ru/rs)$ $1/$2 [L,NS]
Still I'm not sure if you need slash in front of c/wb if you're using this in your .htaccess file. You need slash if you're using this in the VirtualHost configuration.

Resources