htaccess replace character in query and redirect - regex

I need to replace '_' to '+' in query string than redirect:
site.com/abc_def/
to
site.com/search.php?q=abc+def
I tried this
RewriteRule ^([^/]+)/((.*)\_(.*))?$ /search.php?q=$1+$2 [R=301,L]

These are 2 rules that should work for you:
RewriteEngine On
# first replace _ by + recursively
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_(.*)$ /$1+$2 [L]
# once all _s are gone, rewrite to /search.php?q=<search>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]+)$ /search.php?q=$1 [L,QSA]

Related

SEO Friendly URL's Through -> modrewrite

I am attempting SEO friendly-ize my URL's.
Sample URL (that I would like):
http://sampleurl.com/FILENAME/VARIABLE/
What I would like to transpose this to is:
http://sampleurl.com/FILENAME.php?x=VARIABLE
My .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)/$ $1.php?l=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
# Proper regex -> ([a-zA-Z0-9_-]+)
</IfModule>
My challenges are:
1) The trailing slashes: with my regex I have to have a trailing slash. When I change the regex to read ^(.*)/(.*)/?$ - It adds a ".php" to the end of the variable. And, if I leave the ? off - the trailing slash is required - or, it resolves to the "index.php" result
2) Same in the second RewriteRule - with the ? before the slash = Internal Server Error - without the ? = all good.
So everything works now, except I must have that trailing slash.
Seems like I am missing something pretty easy here - and would appreciate someone pointing it out.

Remove trailing slash and create query string with .htaccess

In my .htaccess file, I would like to remove the trailing slash from the URL without modifying the current setup for the query string.
I tried this in a two-step fashion, but it's not working as I expect:
# Remove trailing slash
RewriteRule ^(.+)/$ $1 [R=301]
# Create query string from canonical URL
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Cheers
You can use:
RewriteEngine On
RewriteBase /site/dev/
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

URL rewriting regex with optional capturing

I have these types of URLs:
/manage/123
/manage_foo/456
/manage_bar/789
/manage
/manage_foo
/manage_bar
I need to rewrite to:
/manage.php?id=123
/manage_foo.php?id=456
/manage_bar.php?id=789
/manage.php?id=
/manage_foo.php?id=
/manage_bar.php?id=
How do I make the capturing of the id optional? I know this works if an id is present:
RewriteCond %{REQUEST_URI} ^\/(manage.*)\/(.*)$
RewriteRule ^ %1.php?id=%2 [NC,L]
That regex breaks down if you remove the slash before the id
You can use this rule:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(manage)(?:/([0-9]*))?/?$ $1.php?id=$2 [QSA,L,NC]

Disallow strings in the URL

I'm trying to figure out how to disallow any strings in my URL (I had a problem with an old host allowing ?PHPSESSID strings) and want to avoid any pages with a string to get indexed. Here is my current .htaccess. I've tried a few rules in the beginning that didn't work. Any other thoughts that might force strings back to the last segment?
http://pastie.org/pastes/8660658/text
Have it this way:
RewriteEngine On
RewriteBase /
## force a trailing slash ##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
# strip all query strings
RewriteCond %{QUERY_STRING} .+
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
## BEGIN Expression Engine Rewrite ##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
## END Expression Engine Rewrite ##

Using htaccess to force a trailing slash before the ? with a query string?

I have the following in my htaccess file:
RewriteEngine On
RewriteBase /
# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Trailing slash check
RewriteCond %{REQUEST_URI} !(.*)/$
# Add slash if missing & redirect
RewriteRule ^(.*)$ $1/ [L,R=301]
# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Send to index.php for clean URLs
RewriteRule ^(.*)$ index.php?/$1 [L]
This does work. It hides index.php, and it adds a trailing slash... except when there is a query string.
This URL:
http://example.com/some-page
gets redirected to:
http://example.com/some-page/
but this URL:
http://example.com/some-page?some-var=foo&some-other-var=bar
does not get redirected. I would like for the above to be sent to:
http://example.com/some-page/?some-var=foo&some-other-var=bar
I've reached the limits of my understanding of redirects with this. If you have a working answer, I would really appreciate a walkthrough of what every line is doing and why it works. Double bonus awesomeness for an explanation of why what I have right now doesn't work when there is a query string involved.
Try adding a [QSA] to the end of the last Redirect rule to preserve the original query string as below
# Send to index.php for clean URLs, preserve original query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
a walkthrough of what every line is doing and why it works.
See my comments below
#turn mod_rewrite engine on.
RewriteEngine On
#set the base for urls here to /
RewriteBase /
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### and the URI does not end with a /
RewriteCond %{REQUEST_URI} !(.*)/$
### redirect and add the slash.
RewriteRule ^(.*)$ $1/ [L,R=301]
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to index.php passing the URI as a path, QSA will preserve the existing query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
I believe that if you change this:
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
to this:
RewriteCond %{REQUEST_URI} !^([^?]*)/($|\?)
RewriteRule ^([^?]*) $1/ [L,R=301]
then it should do what you want.
The changes I made are:
In both rewrite-condition and -rule, I changed (.*) and ^(.*) to ^([^?]*), to ensure that, if there's a query-string, then it is not included in either regex. ([^…] means "any character that is not in …", so [^?] means "any character that is not a question mark".)
In the rewrite-condition, I changed $ to ($|\?), so as to match either end-of-URL or end-of-part-before-the-query-string.
In the rewrite-rule, I dropped the $, since it was no longer needed.