Rewrite URL if page exists in a sub folder - regex

I was wondering if there is a way to achieve this sort of URL rewriting;
RewriteRule ^example-link-1?$ /blog/example-link-1 [NC,L]
RewriteRule ^example-link-2?$ /blog/example-link-2 [NC,L]
RewriteRule ^example-link-3?$ /blog/example-link-3 [NC,L]
RewriteRule ^example-link-4?$ /blog/example-link-4 [NC,L]
where you go directly to a link such as example.com/example-link-1 and you are actually shown content from example.com/blog/example-link-1, but without the need for a new rule per page i add. Is there a dynamic way to do this in the htaccess? ie;
IF exists('blog/'+pageURI)
THEN rewrite to 'blog/'+pageURI but keep the uri without 'blog/' in it.

Yes sure you can do:
RewriteRule ^(example-link-[1234])/?$ /blog/$1 [NC,L]
for above 4 rules. But in general you can do this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/blog/$1 -f
RewriteRule ^(.+?)/?$ /blog/$1 [NC,L]
This will rewrite to /blog/something if /blog/something exists while keeping the same URI without /blog/.

Related

.htaccess url rewrite for a specific url

I have a pattern like,
www.example.com/api.php/1,
how do I remove .php from this url,
currently I have this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But it only works if I put www.example.com/api in browser,
not for www.example.com/api/1
not even for www.example.com/api/
You may use a rule like this:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([\w-]+)(/.*)?$ $1.php$2 [L]

Htaccess Public Folder Rewrite Rule

I'm working on a little custom MVC project in PHP and am having some issues with the htaccess. Originally, my htaccess was routing all traffic to index.php in my root dir, and passing the remaining path as an argument. This was working perfectly with this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?path=$1 [NC,L,QSA]
The problem I'm having now is that I need to move the index.php file into a /public directory. I scoured the internet for answers, and found a code snippet that kinda works in that it seems to get there as long as it is just hitting /, but as soon as the url becomes /register/ or anything else it just 404's.
# Get rid of /public/ in the URL, and route all requests through
# the Index.php file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /public/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_URI} !/public/ [NC]
RewriteRule ^(.*?)/?$ public/$1 [L]
I know that last line makes no sense in when there is the rewrite to index.php with ?path that seems proper to me (at least it passes the argument like I want!) but without both these lines it doesn't seem to work, and I've been trial-and-erroring this for hours. Hopefully someone can help out! Cheers!
Keep only this content in your .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^/?$ public/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/?path=$1 [L,QSA]

Redirect and Rewrite to remove .html from url

I have a really basic rewrite rule that I've been banging my head against the wall trying to achieve. I have a static HTML site that I want pretty URLs for.
So, I want /something to serve /something.html and I also want to redirect (externally) from /something.html to /something as to not be penalized on SEO for hosting duplicate content.
I don't want to use Multiviews and I don't want to use <rel cannonical="">
This is what I currently have.
RewriteRule ^(.*)\.html$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
It results in an infinite redirect loop.
It seems to me that the first rule keeps matching even after the requested URL doesn't end in .html.
Yes your rules will cause redirect loop due to use of REQUEST_URI (via RewriteRule) which changes after application of a rule.
You can use:
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.

Value pair url modeling for subpages via htaccess

Ok, currently to get the URL in the format I want I use this in my .htaccess
RewriteRule ^user.php/([0-9]+)/?$ user.php?id=$1 [NC,L]
RewriteRule ^user.php/edit/([0-9]+)/?$ user.php?id=$1&type=edit [NC,L]
Urls, redirect to
domain/user/7/ -> domain/user?id=7
domain/user/edit/7/ -> domain/user?id=7&type=edit
Works fine, but I want to be able to do better value pair urls instead of just strict matches.
An example is
domain/user/key/value/key/value/key/value/
-> domain/user.php?key=value&key=value&key=value
I was looking at
.htaccess rewrite to convert directories into /key/value/key/value
The example works well if you just want to redirect from the main path, ie localhost/index.php, but I want to only be triggered when it's domain/user and I couldn't get it to match with using 'user' as the trigger
My current .htaccess
# Pretty url's
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^user.php/([0-9]+)/?$ user.php?id=$1 [NC,L]
RewriteRule ^user.php/edit/([0-9]+)/?$ user.php?id=$1&type=edit [NC,L]
Is this possible, and if so how?
Deja vu time!!! I wrote that answer sometime back :P
Here is what you can do for your case:
RewriteEngine On
RewriteRule ^(user)/([^/]*)/([^/]*)(/.*)?$ /$1/$4?$2=$3 [L,QSA,NC]
RewriteRule ^(user)/?$ /$1.php [L,NC]
RewriteRule ^user.php/([0-9]+)/?$ user.php?id=$1 [NC,L]
RewriteRule ^user.php/edit/([0-9]+)/?$ user.php?id=$1&type=edit [NC,L]
# Pretty url's
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Using these rules a URL of /user/n1/v1/n2/v2/n3/v3/n4/v4 will be INTERNALLY forwarded to /user.php?n4=v4&n3=v3&n2=v2&n1=v1 treating each pair of URL segments separated by / as a name-value pair forQUERY_STRING`.
Ok this is working for me
RewriteRule ^(user.php)/([^/]*)/([^/]*)(/.*)?$ /$1/$4?$2=$3 [L,QSA,NC]
RewriteRule ^(user)/?$ /$1.php [L,NC]
# Pretty url's
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
URL
/user/n1/v1/n2/v2/n3/v3/n4/v4
Outputs
Array ( [n4] => v4 [n3] => v3 [n2] => v2 [n1] => v1 )
anubhava, you da man :)

How to rewrite url while keeping additional parameters in .htaccess file?

I have the standard htaccess which just catches all and puts it into one url parameter which is later processed in code...
RewriteRule ^config/ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I use "pretty" url params like
/some/route/param1/value1/param2/value2
but now I need an ability to add "regular" url params like
/some/route/param1/value1?param2=value2&param3=value3 etc
I tried adding
RewriteRule ^(.*)\?(.*)$ index.php?q=$1&p=$2 [L,QSA]
before the existing rule, but it won't work properly (and I suspect it would only work with one parameter).
Try this:
RewriteRule ^([^?]+)\?(.*)$ index.php?q=$1&p=$2 [L,QSA]