Shortening URL using .htaccess - php

My folder structure is:
modules
admin
index.php
login
login.php
signup.php
articles
articles.php
My current URL is:
localhost/modules/admin/index.php
localhost/modules/login/login.php
localhost/modules/articles/articles.php
I want to change it to:
localhost/index
localhost/login
localhost/articles
Since i a beginner in using .htaccess file, pls help me how i can change the URL using .htaccess file

Your folder structure makes it difficult to have a universal rule but you should be able to use these rules.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(index)/?$ /modules/admin/$1.php [NC,L,QSA]
RewriteRule ^(login|articles)/?$ /modules/$1/$1.php [NC,L,QSA]

Try this
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteRule ^index([^/]*) /admin/index.php [L]
RewriteRule ^login([^/]*) /login/login.php [L]
RewriteRule ^signup([^/]*) /login/signup.php [L]
RewriteRule ^articles([^/]*) /articles/articles.php [L]

Related

How to remove the page alias from url with htaccess?

I have created my own cms and i find the following issue. The pages links show as this:
https://www.example.com/page/contact-us
I want them to show as this, by removing the page alias:
https://www.example.com/contact-us
My Htaccess:
Options -MultiViews
RewriteEngine On
RewriteRule ^page/([\s\S]*)$ single-page.php?slug=$1 [L]
I have tried this but doesn't work for me
RewriteEngine On
RewriteRule ^([^/]*)$ /page/single-page.php?slug=$1 [L]
RewriteRule ^([^/]*)$ /page/single-page.php?slug=$1 [L]
This won't work because it is forwarding all URIs to /page/ directory which obvious doesn't exist.
You should be using this rule:
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ single-page.php?slug=$1 [L,QSA]

How to ignore htaccess folder from wordpress htaccess

I have this .htaccess file in Wordpress. It's located at /file_name/ (web root). I need to exclude a folder (booknow) from the rewrite engine.
I've tried this code:
RewriteRule ^/booknow($|/) - [L]
My Full .htaccess code given below:
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /file_now/
RewriteRule ^/booknow($|/) - [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /file_now/index.php [L]
</IfModule>
#END WordPress
I tried this code but it didn't work.
Firstly assuming the br in your code are mistakes and not that you're writing HTML inside a .htaccess file.
The pattern in your rewrite rule has both a slash in the RewriteBase and the RewriteRule, so it's looking for something like /file_name//booknow/ You should instead type:
RewriteRule ^booknow($|/) - [L]
I got my own answer. I create a .htaccess File in WordPress directory booknow folder then I added below code to .htaccess file:
RewriteEngine on
RewriteCond %{file_now/booknow} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
ErrorDocument 404 /file_now/booknow/404page.php
Now My Booknow folder is properly working.
Try this code:
RewriteRule ^(directory_name)($|/) - [L]

.htaccess link css is not linking

I am new to .htaccess so bear with me.
I am trying to have my URL look like:
http://example.com/login
Using .htaccess it uses:
http://example.com/index.php?url=login
My .htaccess looks like this:
RewriteEngine On
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The problem with this is that when I try to access http://example.com/asset/style.css it just shows my index.php without being able to use my css. How can I change this to be able to use my css files?
Try
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php?url=$1 - [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Is it possible to allow only the correct pretty url?

I have these pretty urls:
app/category/overview
app/category/new
app/category/edit/4
app/product/overview
app/product/new
app/product/edit/4
.htaccess
RewriteEngine On
RewriteRule ^([a-z]+)/([a-z]+)/?([0-9]*)$ index.phpcontroller=$1&action=$2&id=$3 [NC,L]
Is it possible to define that any other other url goes back to the index?
p.e. app/i/am/the/fastest/man/alive ---> app/
Help would be much appreciated!
You can have a new rule for that:
RewriteEngine On
RewriteBase /app/
RewriteRule ^([a-z]+)/([a-z]+)/?([0-9]*)$ index.php?controller=$1&action=$2&id=$3 [NC,L,QSA]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I have added ? after index.php
The answer to your question
Is it possible to allow only the correct pretty url?
Sorta, but you have to be more specific on your rules to limit them to that. Take this example.
RewriteEngine On
RewriteBase /app
#rewrite rule if starts with /app/product or /app/category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(product|category)/([a-z]+)/([0-9]*)/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
#else redirect to homepage
RewriteRule . app/ [R=301,L]
This will ensure that only URL that are like http://example.com/app/product/1 will be rewritten and all others like http://example.com/app/some/other/page to your home page.

siteproblem in rewriting site urls

i have problems in rewriting url's in my site
when i rewrite url's site styles dont work
i write this code in htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !Resources/css/(.*)
RewriteRule ^/?(index.(html|php|java|jsp|asp))?$ controller.php
RewriteRule ^LoadPage/(.*) controller.php?LoadedPage=$1 [QSA]
RewriteRule ^Admin/(.*) controller.php?Page=Admin&Section=$1 [QSA]
RewriteRule ^(.*) controller.php?Page=$1 [QSA]
but when i write this url for example:
localhost/sitename/Admin/Users
the page Users successfully rendered , but without style !
I think you will find a good answer over here
.htaccess
You can also redirect everything that is not an actual file directory to your controller like so:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . controller.php [L]
</IfModule>

Categories