HTACCESS creating permalinks - .htaccess

I want to rewrite "http://domain.com/url-name/",
to look at "http://domain.com/?url="url-name".
I am trying this but with no success :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]*)\$/ ^([^/]*)?url=$1

There are few mistakes:
In regex, if you escape $ then it will match literal $.
In the target URI, you cannot use regex but provide back-reference to captured value in the pattern.
Use RewriteCond to affect only non-files and non-directories.
Change your code to this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* ?url=$0 [L,QSA]

Related

Regex rewrite rule for filename with dash

I got a filename like jquery.form.min.3.51.0-2014.06.20.js and want to change the following my rewrite rule to remove the dots, the dash and the digits.
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>
The rewrite rule does it jobs on filenames like scripts.min.4.4.2.js which is forwarded to scripts.min.js. But for the filename at the top, with the dash inside between the digits, the rule won't work.
My regex knowledge is too limited for this case. Could anybody give me a hint please?
You can use:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.[\d-]+\.(js|css|png|jpg|gif)$ $1.$2 [L]
</IfModule>
Try the following:
RewriteRule ^(\D+)[\d.-]+(js|css|png|jpg|gif)$ $1$2 [L]

URL Rewrite On .htaccess

I'm trying to make a rewrite rule which will understand
http://example.com/test/1234
as
http://example.com/test.php?t=1234
This is what I have right now and it doesn't work:
RewriteEngine on
RewriteRule ^test?t=([^/\.]+)/?$ http://mywebsite.com/test/$1 [L]
Can someone give me a hand?
Your rewriteRule is backwards in that you're supposed to match on the left what you want the clean url to look like and rewrite on the right to where the file is located on the server. But even if it weren't backwards, you'd have to escape the question mark character in the RegExp. But since it is backwards, you should be using something closer to:
RewriteEngine on
RewriteRule ^test/(.*) test.php?t=$1 [L]
Try :
RewriteEngine On
RewriteCond %{THE_REQUEST} /test.php\?t=([^&\s]+) [NC]
RewriteRule ^test.php$ /test/%1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([^/]+)/?$ /test.php?t=$1 [QSA,L,NC]
Query string is not part of match in rewrite rule directive, Use %{QUERY_STRING} or %{THE_REQUEST} variables to match against the query string.

Url Rewrite not working correctly

I want to change this url from :
Site.com/Controller/myController.php?action=myAction&var1=y&var2=z
to :
Site.com/myAction/var1/y/var2/z/..../varn/valn
Variables can be present or not.
No redirection
I tried many solutions but what i have done is just replacing Controller/myController.php with items:
RewriteEngine on
RewriteRule ^items/(.*)$ Controllers/myController.php$1
thank u
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# recursion to replace /action/n1/v1/n2/v2 to QUERY_STRING
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)(/.*)?$ Controllers/myController.php$2?action=$1 [L,QSA]
RewriteRule ^(Controllers/myController\.php)/([^/]+)/([^/]+)(/.*)?$ $1$4?$2=$3 [L,NC,QSA]

What's wrong with this .htaccess redirect?

I want to redirect (only) mysite/02B /030 etc to theirsite/02B etc.
What's wrong with this? I have the sense that I'm missing something stupidly obvious. :-/
RewriteCond %(REQUEST_URI) ^0(2B|2Y|2C|2Z|2-|30|33)$
RewriteRule ^(.*)$ http://theirsite/$1 [R=301,NC,L]
Thanks!
The leading slash / is included in the REQUEST_URI variable, so you have to include it also in the regular expression to match the variable.
You may try this complete rule-set instead:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/0(2B|2Y|2C|2Z|2-|30|33) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://theirsite/$1 [R=301,NC,L]

How can I redirect all sub-directories to root using htaccess or httpd.conf?

I have an index file that builds content based on n PATH_INFO variables.
Example:
site.com/A/B/n/
should use index.php at either:
site.com/index.php?var1=A&var2=B&varN=n
- or -
site.com/index.php/A/B/n/
instead of:
site.com/A/B/n/index.php || which doesn't exist ||
So far I've tried a number of variations of:
RedirectMatch ^/.+/.*$ /
with no success.
I have an inelegant and unscalable solution here:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p1=$1 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [NC,L]
Problems with this solution:
Inelegant and unscalable, requires manual line for each subdirectory
Fails with non alphanumeric characters (primarily +,= and &) ex. site.com/ab&c/de+f/
(note, even changing the regex to ^([a-zA-Z0-9_-\+\=\&]+)/?$ does little to help and actually makes it error out entirely)
Can you help?
Option 1: (site.com/index.php?var1=A&var2=B&varN=n):
Options +FollowSymLinks -MultiViews
RewriteEngine On
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteRule ^([^/]+)/?$ index.php?p1=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [QSA,L]
1. You had [NC] flag ... so there were no need to have A-Z in your pattern.
2. Instead of [a-zA-Z0-9_-\+\=\&] or [a-zA-Z0-9_-] I use [^/] which means any character except slash /.
3. [QSA] flag was added to preserve existing query string.
Option 2: (site.com/index.php/A/B/n/):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]
In reality, if you do not plan to show that URL anywhere (like, 301 redirect etc), the last line can easily be replaced by RewriteRule .* index.php [L] -- you will look for original URL using $_SERVER['REQUEST_URI'] in your PHP code anyway.
Adding the following will redirect all traffic (for files that do not exist) to the index page:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
Then, you can make your decision in index.php by parsing the 'REQUEST_URI'
RewriteRule ^/.+ / [R=301,L]
any url with any kind of path beyond root will be redirected to root.
^ = start of url
/ = a slash
.+ = 1 or more characters

Resources