Two different pages/files in the .htaccess - .htaccess

I'm getting to know more about .htaccess, Apache & httpd config file. I have 2 files:
index.php
page.php
Both in the same folder
I already succeed to rewrite the index.php?site= to example.com/sitename. But when I try to add a rewrite which has to look like example.com/sitename/article/2 which redirects to page.php?site=sitename&page=pageid,
the rewrite does not work. I think this is because of the previous redirect example.com/sitename
But I like to have them both in my .htaccess file.
My current code is
RewriteEngine On
RewriteRule ^(admin)($|/) - [L]
RewriteRule ^([a-zA-Z0-9-/+]+)$ index.php?site=/$1 [L,QSA]
RewriteEngine On
RewriteRule ^(admin)($|/) - [L]
RewriteRule ^([a-zA-Z0-9-/+]+)$ index.php?site=/$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-/+]+)$ index.php?site=/$1/Article/&page=$2 [L,QSA]
As you can see in the last code example I added has to go to:
example.com/Sitename/Article/2

Related

Rewrite htaccess url and remove extension

I'm trying to rewrite and remove extension
http://www.example.com/pages/cart.php
to
http://www.example.com/cart
My htaccess file is located in the public folder.
RewriteEngine On
RewriteRule (.*) pages/$1 [L]
With your shown attempts, please try following htaccess Rules. Make sure you keep your htaccess file along with pages folder(not inside it).
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/pages/(cart)\.php\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^(.*)/?$ pages/$1.php [L]

User friendly URL through .htaccess

My url looks like -
http://localhost/user_notes/?id=1234.
I want to be turn it into user friendly url like
http://localhost/user_notes/1234
My .htaccess file looks like -
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
RewriteRule ^(.+)$ /user_notes/?id=$1
</IfModule>
The problem is with RewriteRule ^(.+)$ /user_notes/?id=$1
For more info see my directory structure
Note : In my directory structure there is also a public folder. But I can access files without including public in URL through htaccess file (line 3).
Could you please place these rules at top of your htaccess file(I hope there are NO redirect rules for https in your htaccess file). Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^ public%{REQUEST_URI} [L]
RewriteRule ^(user_notes)/(.*)/?$ $1/?id=$2 [NC,L]

Redirect to different `index` file when API is in the URL with .htaccess?

I am struggling a lot trying to redirect API calls to a different index file.
I want to redirect all URL's that have [domain-name]/API/* in the URL to a different script: API_index.php.
I tried doing it with the following in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} /API/
RewriteRule . API_index.php [L]
RewriteRule ^(font-awesome|fonts|ajax|tools|log|documentation)($|/) - [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav)$ index.php [NC,L]
According to this website this URL, http://abayocms.dev/API/test, should match, but I am still being navigated to index.php?
ANSWER
Apparently the .htaccess [L] only stops the current iteration over the URL.
Once I redirected to the API_index file with my first rewriteCond + Rule, the server starts a new iteration with the new URL, which would be [some domain]/API_index.php.
This gets catched by my last rule and still gets redirected to index.php
Thus this scenario needs its own rule:
RewriteRule ^API_index.php$ - [L]
The total file looks like:
RewriteEngine on
# Prevents redirecting API_index.php to index.php
RewriteRule ^API_index.php$ - [L]
RewriteCond %{REQUEST_URI} /API/
RewriteRule . API_index.php [L]
RewriteRule ^(font-awesome|fonts|ajax|tools|log|documentation)($|/) - [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav)$ index.php [NC,L]
Big thanks to #starkeen for the answer!
It's because /API_index.php is matched by your 3rd rule and gets rewritten to /index.php .
You need to pass the /API_index.php untouched, try adding the following after RewriteEngine on
RewriteRule ^API_index.php$ - [L]

Mod_rewrite - redirect old urls to the new ones

I'm looking for a solution to redirect old urls (e. g. aboutme.php) to the new ones (same /about-me).
The problem is: if I'll go to example.com/aboutme.php, the user is not redirected to pretty url (/about-me). Adding R=301 doesn't help - it makes /about-me redirect to aboutme.php.
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^about-me$ /aboutme.php [L]
RewriteRule ^portfolio$ /portfolio.php [L]
RewriteRule ^motion$ /motion.php [L]
RewriteRule ^contact$ /contact.php [L]
RewriteRule ^contact-thanks$ /contact_thanks.php [L]
</IfModule>
You have to make it in the other direction.
If you want to rewrite aboutme.php to about-me
you should use
RewriteRule ^aboutme.php /about-me [R=301,L]
EDIT:
The place /about-me has to be present. If not, you can redirect this virtual place to a file (say rewrite.php) which gets the actual file from database or some configuration. Place the following rules at the bottom of your htaccess rules.
#don't rewrite the rewrite script
RewriteRule rewrite.php - [L]
#as example don't rewrite the image folder
RewriteRule ^images - [L]
#everything else that doesn't match a rule
RewriteRule ^(.*)$ rewrite.php?file=$1 [QSA,L]
after that you can access your requested virtual file in rewrite.php with $_GET['file'].
EDIT:
your rewrite.php could look like this: (this is the simplest way todo. but you have to do some error checking etc.)
<?php
$file = $_GET['file'];
$config = array(
'about-me' => 'aboutme.php'
);
include($config[$file]);
?>

Redirect url with .htaccess

I m sure that many people will say that this is duplicated but I try everything from other "Question"`s and nothings work for me.
The problem is that I move my project to the web server.
In this server I have folder "public_html" where I but my project Symfony.
Now to enter on my project I must write the following url: www.mydomain.com/Symfony/web/*
But I want to write a Rewrite Rule which will redirect from www.mydomain.com/Symfony/web/* to
www.mydomain.com/home/*.
To do this I try on 2 different ways with many combination of ReWrite rule.
In my public_html I create a .htaccess folder
I edit the .htaccess in Symfony/web folder
I add the following rule in both file but without success
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Symfony/web/(.*)$ www.mydomain.com/home/$1 [L,R=301]
Unfortunately without success. What I`m doing wrong?
My htaccess file look like
And all the time Error 404 Object not found
Symfony/web/.htaccess
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Symfony/web/
RewriteCond %{THE_REQUEST} \s/+Symfony/web/ [NC]
RewriteRule ^(.*)$ /home/$1 [L,R=301]
RewriteRule ^home/(.*)$ $1 [L,NC]
</IfModule>
It`s redirecting me but I receive again Object not found :(
I delete the .htaccess in public_html folder which is the root one for my server
public_html\.htaccess
RewriteEngine On
RewriteRule ^home/(.*)$ /Symfony/web/$1 [L,NC]
1: Place this code in /Symfony/web/.htaccess:
RewriteEngine On
RewriteBase /Symfony/web/
RewriteCond %{THE_REQUEST} \s/+Symfony/web/ [NC]
RewriteRule ^(.*)$ /home/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [L]
2: Place this code in /public_html/.htaccess:
RewriteRule ^home/(.*)$ /Symfony/web/$1 [L,NC]
I'm going to go out on a limb and say that your rule is backwards. I think you want your URL to be www.mydomain.com/home/* in the browser... In which case the rule would be reversed. Also, your .htaccess should be in the root and you don't need to include the domain in the rewrite rule because you set a rewrite base.
RewriteRule ^home/(.*)$ Symfony/web/$1 [L,R=301]

Resources