htaccess rewrite rules on single page - regex

I want to change:
domain/link to domain/index.php?q=link
and
domain/notice/20151212/title-with-a to domain/index.php?q=notice&date=20151212&title=Title-with-a
with this .htaccess file:
RewriteEngine On
RewriteRule ^([a-zA-Z-]+)$ index.php?q=$1 [L]
RewriteRule ^notice/([0-9]+)/([A-Za-z0-9_-]+)$ index.php?q=notice&date=$1&title=$2 [L]
If i enter into domain/link or domain/link2, goes well but if i try to enter to domain/notice/20151212/title-with-a, I can't because it cant get CSS, image, JS files (tries to get domain/notice/20151212/js/theme.js when the file is in domain/js/theme.js) but i can enter without problems by the URL:
domain/index.php?q=notice&date=20151212&title=Title-with-a
What am I doing wrong?
I check this regex on http://htaccess.mwl.be/ and i think the htaccess file is right.
Thank you.

try to do this
RewriteEngine On
RewriteRule ^notice/([^/]+)/([^/]+)/$ index.php?q=notice&date=$1&title=$2 [L]
RewriteRule ^([a-zA-Z-]+)$ index.php?q=$1 [L]
and try to revert order like here
RewriteEngine On
RewriteRule ^notice/([0-9]+)/([A-Za-z0-9_-]+)$ index.php?q=notice&date=$1&title=$2 [L]
RewriteRule ^([a-zA-Z-]+)$ index.php?q=$1 [L]
EDIT
add [L] at the end of second line and try my url, it works
http://lab.maurosala.it/notice/a/b/
if you don't want the / at the end of url use this
RewriteRule ^notice/([^/]+)/([^/]+) notice.php?q=notice&date=$1&title=$2 [L]

Related

htaccess proper redirect with trailing slash

Good day!
I want to make proper redirect by htaccess, when there is GET request like this:
example.org/directory/page/
It should give example.org/page.php
My htaccess lets only redirect this kind of request
example.org/directory/page
I use this config
#remove extension
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#remove directory
RewriteRule ^directory/(.*)$ /$1 [L]
Thank you for attention!
Reorder your rules and tweak .php adding rule:
#remove directory
RewriteRule ^directory/(.*)$ /$1 [L]
#remove extension
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^.]+)/?$ $1.php [L]

How can I redirect specific parts of a URL to a new URL using htaccess?

In building a new site I need to restructure some of the links, and I need to redirect the old links to the new links. I have only a few, and are as follows:
about|what-we-do => about-us
contact => get-in-touch
our-work|projects => featured-work
I want to be able to keep anything they put after those links, also. For example, I'd like the redirects to be like this:
our-work/web/project-title => featured-work/web/project-title
This is what I have in my htaccess file at this point, which doesn't work.
RewriteRule ^(about|what-we-do)(/)? /about-us/$1 [QSA,NC,L,R=301]
RewriteRule ^contact(/)? /get-in-touch/$1 [QSA,NC,L,R=301]
RewriteRule ^(our-work|projects)(/)? /featured-work/$1 [QSA,NC,L,R=301]
RewriteRule ^about-us(/)?$ /pg.about.php [QSA,NC,L]
RewriteRule ^get-in-touch(/)?$ /pg.contact.php [QSA,NC,L]
RewriteRule ^featured-work(/)?$ /pg.projects.php [QSA,NC,L]
RewriteRule ^hello(/)?$ /pg.hello.php [QSA,NC,L]
If I try and visit something like /about then I'm redirected to /about-us/about/. I'm not well-versed in htaccess. So, if anyone is able to help me, it would be much appreciated. I'll keep plugging away at it. Thanks in advance!
Your rules are not capturing URI part after first slash. You can use:
RewriteEngine On
RewriteRule ^(?:about|what-we-do)(/.*)?$ /about-us$1 [NC,L,R=301]
RewriteRule ^contact(/.*)?$ /get-in-touch$1 [NC,L,R=301]
RewriteRule ^(?:our-work|projects)(/.*)?$ /featured-work$1 [NC,L,R=301]
RewriteRule ^about-us/?$ /pg.about.php [NC,L]
RewriteRule ^get-in-touch/?$ /pg.contact.php [NC,L]
RewriteRule ^featured-work/?$ /pg.projects.php [NC,L]
RewriteRule ^hello/?$ /pg.hello.php [NC,L]

.htaccess RewriteRule Conflict Error

I have htaccess file problem. I think my htaccess files conflict.
my .htaccess files:
RewriteEngine on
RewriteRule ^(.*)/video/ video.php?vid=$1 [L]
RewriteRule ^search/(.*)\.html search.php?q=$1 [L]
RewriteRule sitemap-(.*).xml sitemap.php?sayfa=$1 [L]
and my url
Search : http://www.domain.com/search/search_query.html
Video Play : http://www.domain.com/path1/video/video_name.html
.htaccess file conflict video.php and search.php
pls help me
Change order of your rules to keep specific ones first and generic ones later:
RewriteEngine on
RewriteRule ^search/(.*)\.html$ search.php?q=$1 [L]
RewriteRule sitemap-(.*).xml$ sitemap.php?sayfa=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/video/ video.php?vid=$1 [L,NC]
Try using a negative lookahead to prevent the two rules from getting applied to each other:
RewriteEngine on
RewriteRule ^(?!search)(.*)/video/ video.php?vid=$1 [L]
RewriteRule ^search/(?!video)(.*)\.html search.php?q=$1 [L]

URL Regex - remove trailing slash from file name and end of URL?

So I have this regex problem and wondered if anyone could help me out?
If a user visits http://example.com/index.php/ how can i modify/add to my regex to prevent a trailing slash(s) at the end?
also
I currently have a page, called post.php that can be accessed like so http://example.com/reviews/reviewTitle/ and http://example.com/news/newsTitle/
again, how could I prevent this trailing slash?
Below is the regex I have so far:
RewriteEngine on
RewriteRule ^reviews/([^/\.]+)/?$ reviews/post.php?title=$1 [L]
RewriteRule ^news/([^/\.]+)/?$ news/post.php?title=$1 [L]
RewriteRule ^page/(.*) index.php?page=$1
Note: Im also re-writing http://example.com/index.php?page=1 to http://example.com/page/1 etc, same question, how can I prevent a trailing slash?
Many thanks, I really appreciate any help :)
Try adding this rule after RewriteEngine on
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301]
RewriteRule ^reviews/([^/\.]+)$ reviews/post.php?title=$1 [L]
RewriteRule ^news/([^/\.]+)$ news/post.php?title=$1 [L]
RewriteRule ^page/(.*) index.php?page=$1
Edited to show full set of rules
Edited 2nd Time Added in
RewriteCond %{REQUEST_FILENAME} !-d
above new rule to allow directorys to still be accessed without causing an infinite redirect loop

Mod rewrite question (really a regex problem)

Ive started out with the most basic rewrite there is, take any request and give it to my index page:
RewriteEngine On
RewriteRule ^.*$ index.php [L,QSA]
Im trying to change it now so lets say i have a directory called special, I actually want to be able to go to http://example.com/special and access whatever files are in there.
I tried this:
RewriteRule ^special/.*$ index.php [L, QSA]
but it didnt work.
Try either this:
RewriteRule !^special/ index.php [L]
Or this:
RewriteRule ^special/ - [L]
RewriteRule ^ index.php [L]
If you want this to apply to any directory, try this:
RewriteEngine On
RewriteCond %{REQUESTFILENAME}!-d
RewriteRule ^ index.php [L]