.htaccess clean URLs with slugs 404 not found - regex

I apologize for asking this question once again, but I'm unable to make my URLs work without getting an 404 page error.
public_html
.htaccess
webroot+
index.php
article.php
I'm using htaccess to make the webroot folder the main document root ( I do not have access to virtual hosts)
RewriteCond %{HTTP_HOST} ^site.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !webroot/
RewriteRule (.*) /webroot/$1 [L]
I'm not sure if the issue is caused due to the code above but when I try making my URLs clean for seo I get an 404 page.
I'm trying to change www.site.com/article.php?article_uid=number&article_title=title ---> www.site.com/article/id/title/
With the following code it allows the page to change the url but it gives me an 404 not found.
RewriteCond %{THE_REQUEST} \ /article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=301,NE]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^article/([^/]+)/([^/]+)/?$ /article.php?article_uid=$1&article_title=$2 [L,QSA]
ex.
The requested URL /webroot/article/id/title/ was not found on this server.

Change last rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ webroot/article.php?article_uid=$1&article_title=$2 [L,QSA]

Related

Htaccess rewritting non www to www /and/ domain.tld/id to domaine.tld/page.php?id=

I can't find solution to do 2 things on my htaccess for my domain :
1/ -> Redirect https://domain.tld to https://www.domaine.tld (index.php)
2/ -> Url rewritting to have url like https://www.domaine.tld/id witch call https://www.domain.tld/page.php?id=
My htaccess :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.tld [NC]
RewriteRule ^(.*)$ https://www.domain.tld/$1 [L,R=301]
# Redirection page.php?id=
RewriteRule ^([0-9a-zA-Z\-]+?)?$ /page.php?id=$1 [L]
With this htaccess I'm redirected to page.php?id=$1 but if I try to go on index I'm always redirected to page.php
I know the 2 conditions are the same but I don't know how could I fix it.
An idea ?
Thank you :)
With your shown attempts, please try following rules in htaccess file. Make sure your htaccess file is residing along with index.php, page.php files.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect with www attached in url.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
##Internal rewrite for home pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?$ index.php [QSA,L]
##Internal rewrite for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ page.php=$1 [QSA,L]

htaccess File Rewriting API Routes

I am trying to redirect all incoming traffic to my site to index.php and put the remainder of the url in a path variable. I also have a index.html in my root directory and if the user accesses it directly I want them rerouted to the index.php script.
RewriteEngine On
RewriteBase /
RewriteCond $1 !^index\.html
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
The rerouting seems to be working ok, but all my /api/index.php?route=xx&mode=xx seem to be getting rerouted too. Is there a way to exclude the api directory from the rewrite condition?
Could you please try following. This is checking if REQUEST_URI is not starting from /app/index.php then reroute everything to index.php along with passing parameters to it, couldn't test it as of now, will test it few mins or so.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/index\.php/?$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/api/index(\.html)/?$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]

htaccess redirect specific TLD to subfolder while preserving existing root-to-subfolder redirect

I have a domain example.com which hosts an English version of a website on example.com/en-US/ and a French version on example.com/fr-FR/
Until now we have used the English version as default, so we would redirect everybody requesting example.com to example.com/en-US/ using these rules in the .htaccess file:
Redirect 301 /index.html http://examle.com/en/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
So when you request http://exmaple.com in your browser, the server will request the index.html and that will trigger the Rediret to the /en-US/ directory.
Now we got the French example.fr TLD - pointing to the same webspace. I am now looking for a way to reirect the browser request http://example.fr to http://example.com/fr-FR/
I have tried adding the following condition, but it results in some kind of loop adding lots of /en-US/s into the URL.
Please help me, I need a way to redirect
hxxp://example.com --> hxxp://example.com/en-US/
hxxp://example.fr --> hxxp://example.com/fr-FR/
and all people explicitly requesting hxxp://example.com/index.html --> hxxp://example.com/en-US/ too
You can use this rule based on condition based on HTTP_HOST:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?:index\.html)?$ /en-US/ [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.fr$ [NC]
RewriteRule ^(?:index\.html)?$ /fr-FR/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L,NE]

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]

remove folder from url using .htaccess

I know there are plenty of other questions similar to this one, i tried followinf some of those, but with no luck.
I have a website called test.com and i need, when someone seraches for test.com to show the content in test.com/home, without having home in the uerl.
My htaccess file at the moment looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.*) home/$1 [L]
RewriteRule /home/(.*) /$1 [L,R=302]
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
but when i type test.com it shows a list of folders, not the content inside home.
What am i doing wrong?
thanks
You can simplify your rules to this in root .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^$ home/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]
Make sure to clear your browser cache before testing this.