I'm trying to create some rules using .htaccess:
i would like to do:
blog/[specific-page-title] : should be a mask of src/components/blog/index.php
[category]/[specific-module-url] : should be a mask of ./index.php
[specific-module-url] : should be a mask of ./index.php
The .htaccessfile content is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RULES FOR BLOG
RewriteRule blog/(.+)$ ./src/components/blog/index.php?url=$1 [L,QSA,NC]
# RULES FOR LANDING PAGE
RewriteRule ([a-z\-_0-9]+)/([^/.]+)$ index.php?module=$1&url=$2 [QSA,NC]
RewriteRule ([^/.]+)$ index.php?url=$1 [QSA,NC]
The problem is
Always it's redirect to ./index.php - i can't access blog.
How i can make the rules not be overwriten?
You're missing the ^ start anchor on your rules so they match URLs that they shouldn't:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RULES FOR BLOG
RewriteRule ^blog/(.+)$ ./src/components/blog/index.php?url=$1 [L,QSA,NC]
# RULES FOR LANDING PAGE
RewriteRule ^([a-z\-_0-9]+)/([^/.]+)$ index.php?module=$1&url=$2 [QSA,NC]
RewriteRule ^([^/.]+)$ index.php?url=$1 [QSA,NC]
Related
i want to change url by htaccess from http://localhost/projectname/cab-details.php?v_name=variable to http://localhost/projectname/variable Here variable is a link(slug) Now My htaccess code is
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^$1/([a-zA-Z-0-9-]+) cab-details.php?v_name=$1
With your shown samples please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /omm/
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/(omm/[^.]*)\.php\?v_name=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal redirect rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^omm/([^/]*)/([^/]*)/?$ $1.php?v_name=$2 [QSA,NC,L]
I have the below rewrite but I want to replace the URL without extension .php if the user types the URL with .php at last, how can I revise the below rewrite script?
# /m/yyy rule
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [L,NC]
# /m/yyy/abc rule
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [L,NC]
# /m/yyy/abc/ rule
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [L,NC]
With your shown samples, please have your .htaccess file with following Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
# metchants/yyy/index.php to redirect /m/yyy rule
RewriteRule ^merchants/([\w-]+)/index\.php/?$ m/$1 [R=301,L,NC]
# merchants/yyy/abc.php to redirect /m/yyy/abc rule
RewriteRule ^merchants/([\w-]+)([\w-]+)\.php/?$ m/$1/$2 [R=301,L,NC]
# merchants/yyy/xyz/abc.php to redirect /m/yyy/xyz/abc rule
RewriteRule ^merchants/([\w-]+)/([\w-]+)/index\.php/?$ m/$1/$2 [R=301,L,NC]
# To handle /m/yyy/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [QSA,L,NC]
# To handle /m/yyy/abc/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [QSA,L,NC]
# To handle /m/yyy/xyz/abc/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [QSA,L,NC]
I have a url like
localhost/case?id=name
How do I hide the id part without routing by using htaccess and just show:
localhost/case/name
p.s. framework phalcon
htaccess have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
Try this:
#if our requested file isn't a dir, and isn't a file, and isn't a symbolic link
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#then skip our next rule below
RewriteRule .* - [S=1]
#since we got here, the file exists, so don't rewrite it, skip the next two rules
RewriteRule .* - [S=2]
#these two rules are bound to our condition i.e. if the link is broken
RewriteRule ^case/([^/]*)$ index.php?_url=/case&id=$1 [QSA,L]
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
#end of our two rules which would be skipped if our condition was false
Give this a try:
RewriteEngine On
RewriteRule ^case/([^/]*)$ /case?id=$1 [L]
I have a problem with htaccess on one of these 2 pages: index.php & search.php
Both pages are functional & dependent on variables being sent to them via "GET" method.
SEARCH Example -
search.php?tag=KEYPHRASE
Showing as: site.com/tag/KEYPHRASE
# HTACCESS:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^tag/(.*)/?$ /search.php?tag=$1 [NC,L]
# internal forward
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/(.+?)/?$ /search.php?tag=$1 [L,QSA,NC]
# external rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?tag=([^\s]+) [NC]
RewriteRule ^ /tag/%1? [R=302,L]
INDEX Example -
index.php?id=ID&title=TITLE
Showing as: site.com/cover/TITLE-ID
# HTACCESS:
RewriteRule ^cover/(.*)-(.*)/?$ index.php?id=$2&title=$1 [NC,L]
# internal forward FOR SINGLE PAGE (index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/(.+?)-(.+?)/?$ /index.php?id=$2&title=$1 [L,QSA,NC]
# external rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s]+)&title=([^\s]+) [NC]
RewriteRule ^ /cover/%2-%1? [R=302,L]
Both pages also have pagination, my problem is, it only works for search.php
SEARCH page w/ pagination:
site.com/tag/KEYPHRASE&page=# WORKS! Correct paginated results being returned.
INDEX page w/ pagination:
site.com/title&page=#-ID Inserts &page=# in between the title & ID in the URL, but shows results.
The only difference I see between these 2 are the number of variables being passed on, otherwise they are being rewritten exactly the same.
I need help with making my .htaccess structure work seamlessly w/ pagination query strings for my index.php page URL.
You need to use QSA flag in your internal rewrite rules and rearrange your rules.
Keep your .htaccess like this:
RewriteEngine On
# external rewrite
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^&\s]+)&title=([^&\s]+)&page=([^&\s]+) [NC]
RewriteRule ^ /cover/%2-%1/%3? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^&\s]+)&title=([^&\s]+)\s [NC]
RewriteRule ^ /cover/%2-%1? [R=302,L]
# external rewrite
RewriteCond %{THE_REQUEST} \s/+search\.php\?tag=([^&\s]+)&page=([^&\s]+) [NC]
RewriteRule ^ /tag/%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+search\.php\?tag=([^&\s]+)\s [NC]
RewriteRule ^ /tag/%1? [R=302,L]
# internal forward FOR SINGLE PAGE (index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/([^-]+)-([^/]+)/([0-9]+)/?$ /index.php?id=$2&title=$1&page=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/([^-]+)-([^/]+)/?$ /index.php?id=$2&title=$1 [L,QSA,NC]
# internal forward
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/([^/]+)/?$ /search.php?tag=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/([^/]+)/([0-9]+)/?$ /search.php?tag=$1&page=$2 [L,QSA,NC]
I would like to have multiple RewriteRules in my .htaccess file.
# Enable Rewriting
RewriteEngine on
# Rewrite profile urls
# Input: /user<userId>
# Output: /profile.php?id=<userId>
RewriteRule ^user(\d+)/?$ profile.php?id=$1 [L]
# Rewrite by default to redirect.php
RewriteRule .* redirect.php
Every requests points to redirect.php
I thought, with the [L] flag in the first RewriteRule, would stop processing the rule set.
If you would like to rewrite /user<userId> to /profile.php?id=<userId> and rewrite the other URLs to /redirect.php, then you could try these two configuration directives:
RewriteEngine on
RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
RewriteRule .* /redirect.php
OR:
RewriteEngine on
RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /redirect.php
Adding the following to the second RewriteRule works.
# Rewrite by default to redirect.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* redirect.php
But im not sure, why this is needed.
[L] flag stops processing only in current iteration but mod_rewrite repeat process until path will not be constant.
In your case I think you can use this rule:
RewriteRule ^user\d+$ profile.php
RewriteCond %{REQUEST_URI} !profile.php
RewriteRule ^.*$ redirect.php