how to sepratly on admin folder in htaccess - regex

Friends i am php developer i did customize the url in example is
From : http://example.net/page.php?post_id=contact/
To : http://example.net/contact/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteBase use only home/phtml/www/ this kind of path
RewriteRule ^([a-zA-Z-/]+)$ page.php?post_id=$1 [QSA]
RewriteRule ^([a-zA-Z-/]+)/$ page.php?post_id=$1 [QSA]
</IfModule>
its working fine on above that htaccess code but it have some problem. What is i got "admin" folder for control panel purpose. In which how to redirect to http://example.net/admin/ correctly any option this in htaccess?
My error is: http://example.net/page.php?post_id=admin/
How to Solve this?

You can skip all directories from your rewrite rule. Use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z/-]+)/?$ page.php?post_id=$1 [L,QSA]
</IfModule>
Also you should keep unescaped hyphen at first or last place in a character class.
I combined 2 of your rules into one using optional trailing slash.

Related

Why URL parameter is not working rewrite URL htaccess?

I am trying to get blog details page with rewriting URL
like this ->>
www.sitename.com/blog/test-post
My code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?([a-z]+) details.php [L]
</IfModule>
My problem is , it is coming the details page, but the blog list page
www.sitename.com/blog/
also redirecting to blog details page. how can i avoid this. i also tried with number code like this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?([0-9]+) details.php [L]
</IfModule>
the url with number parameter works fine, but letters is not working. how to solve this?
You need to skip files and directories from your rule:
<IfModule mod_rewrite.c>
RewriteEngine on
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . details.php [L]
</IfModule>

HTACCESS redirecting to wrong page

When user enters url http://localhost/systems/admin in address bar I need to redirect user to page http://localhost/systems/admin/login/
It works fine when enter /admin
It does not work properly when I enter /admin/ (note trailing slashes), it redirects me to some other page http://localhost/systems/user/login but I want it to redirect me to same page i.e. http://localhost/systems/admin/login/
Below is my entire .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /systems/
RewriteRule ^admin$ http://localhost/systems/admin/login/ [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
With your .htaccess configuration you are redirecting only /admin to /admin/login/.
To support a possible trailing slash you should go for something like:
RewriteRule ^admin/?$ http://localhost/systems/admin/login/ [R=301,L]
Generally speaking I would stick to only one notation (either with or without the trailing slash) and add a rule that always corrects wrong notations (i.e. adding or removing the trailing slash). A rule to remove the trailing slash could be:
RewriteRule ^(.*)/$ /$1 [L,R=301]
With such a rule you can stick to your original rewrite rule. In this case, however, you should always take care of returning addresses without the trailing slash in order to avoid rewriting when not necessary:
RewriteRule ^admin$ http://localhost/systems/admin/login [R=301,L]

.htaccess redirect old html page with space in the name

I've changed an html site to a WordPress installation.
The old .html links have been redirected to the right wordpress page. The old site, which was obviously build by a monkey, had a page /over ons.html. That's right, it had a space in the filename. So now, i'am getting external requests on /over%20ons.html. I can't figure out how i can solve this redirection problem in .htaccess. The page it has to refer to is /over-ons/.
This is my .htaccess so far:
RedirectMatch 301 ^/([^/.]+)\.html$ /$1/
Redirect 301 /over%20ons.html http://www.sterkermerk.nl/over-ons/
Redirect 301 /index.html http://www.sterkermerk.nl/
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The 2nd line contains the rule. I've tried to escape the %20, tried using regex in RedirectMatch, but I can't get it to work.
Anyone got an idea?
Use mod_rewrite instead of mod_alias (Redirect statements) because you're already using mod_rewrite and they'll conflict with each other:
RewriteRule ^over\sons\.html$ http://www.sterkermerk.nl/over-ons/ [L,R=301]
RewriteRule ^index\.html$ http://www.sterkermerk.nl/ [L,R=301]
RewriteRule ^([^/.]+)\.html$ /$1/ [L,R=301]
(replacing the Redirect* statements)
The URI is decoded before being sent through rewrite rules so you don't need to use the %20, just \s or \ will match the space.

htaccess clean urls & replacing whitespaces and %20 with -

I'm strugling to make this work. At the moment my htaccess contains the following code:
#Debugging - Error reporting
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
#Commpression
<ifmodule mod_deflate.c="">
<filesmatch ".(js|css|html|png|jpg|jpeg|swf|bmp|gif|tiff|ico|eot|svg|ttf|woff|pdf)$"="">
SetOutputFilter DEFLATE
</filesmatch>
</ifmodule>
Options All -Indexes +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
#RewriteCond %{THE_REQUEST} (\s|%20)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(png|jpg|bmp|gif|css|js)$ [NC]
RewriteRule ^([^/]+/?.+)$ /index.php?req=$1 [L,QSA]
</IfModule>
Everything works great except 1 thing if I try this url for example:
http://www.domain.com/ test/
the browser translates it like to: http://www.domain.com/%20test/
basically after the domain if the path starts with a whitespace or a %20 it fails.
can anyone please point to a solution where the starting spaces will be removed ?
UPDATE
The goal:
www.domain.com/ this is a test / hello there /
or
www.domain.com/ this is a test
to
www.domain.com/this-is-a-test/ or www.domain.com/this-is-a-test/hello-there
I am guilty of writing that code more than 2 years back :P
That can be hugely simplified by this code:
# remove spaces from start or after /
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]
# remove spaces from end or before /
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]
# replace spaces by - in between
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R]
PS: Must add that you need to fix the source of these URLs also because it is really not normal to be getting URLs like this.
This is works fine
<IfModule pagespeed_module>
ModPagespeed on
ModPagespeedEnableFilters collapse_whitespace,remove_comments
</IfModule>

mod_rewrite rewrite_rule syntax question

I have the following in my .htaccess currently-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^go/([^/]*)/([0-9]+)/([0-9]+)/?$ http://foo.com/wp-content/plugins/foo/cloak.php?post_id=$2&link_num=$3&cloaked_url=$0 [L]
RewriteRule ^go/([^/]+)[/]?$ http://foo.com/wp-content/plugins/foo/cloak.php?name=$1&cloaked_url=$0 [L]
</IfModule>
I want to change it so that the domain 'http://foo.com' is auto-detected and inserted (or just left off if it's unnecessary.
I'm hoping to use this .htaccess to manage multiple mapped domains to the same code base and can't really have it work with a specific URI in there.
Use RewriteCond for that:
RewriteCond %{HTTP_HOST} !^foo.com$
RewriteRule ...