Cut folder path from URL via .htaccess - php

I'm trying to shorten my URL but sadly can't find anything that helps.
I divide my code in folders. Index is positioned at root just like my .htaccess.
The folders are named like the file extensions, so php, js, css [...]
I have a link like the following:
localhost/php/getBets.php
and want it to be
localhost/getBets/
I already have the part that cut's the .php extension at the end, so here is my full .htacces
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Hide Index
IndexIgnore *
# Forbid accessing certain sites
RedirectMatch 403 ^/.gitignore$
RedirectMatch 403 ^/.htaccess$
RewriteRule ^(?!index)(?!.*getBets).*\.(php|rb|py|txt|md|sql|inc)$ - [F,L,NC]
# Hide .php file ending in URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Can someone maybe tell me how I could achieve this? :)
Thanks alot!

For your required url you can use below rule in root directory it is for rewriting,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ php/$1.php [L]

Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
Use %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]

Related

Friendly Url don't get the link [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

PHP redirect to another page with dynamic url and get data [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

htaccess for rewrite URL is not working for my website

I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]

mod_rewrite - add middle tier directory

Pages are currently available at the following addresses:
http://www.domain.co.uk/solutions/article/page-name1
http://www.domain.co.uk/solutions/article/page-name2
http://www.domain.co.uk/solutions/article/page-name3
etc
..but I would like them to be accessible via the following addresses in order for enhanced SEO:
http://www.domain.co.uk/solutions/page-name1
http://www.domain.co.uk/solutions/page-name2
http://www.domain.co.uk/solutions/page-name3
So, I think I want to use mod_rewrite in the .htaccess file of the site root to add the 'article' directory after the solutions directory on each incoming page request. I have been looking for a suitable answer for about a month and I've tried learning the mod_rewrite basics tutorials but I just can't make this work for me so apologies for another mod_rewrite question.
This is my .htaccess file at present:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
I'm doing something similar on a site of mine. Here's the .htaccess rules I'm using to remove index.php and show the shortened url:
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(css|js|gif|jpe?g|png|ico) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
RewriteCond %{REQUEST_URI} ^/solutions/(.*) [NC]
RewriteRule ^(.*) /solutions/article/%1 [L]
Remember that the URL you see in your browser's address bar is not the URL that ExpressionEngine sees - EE sees {segment_2} as "article".
Edit :
Try this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^solutions/article/
RewriteRule ^solutions/(.*)$ /solutions/article/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

How can I use mod_rewrite to remove the query string from my URL?

I am new at mod_rewrite and need some help if is possible.
this is my old url
www.mysite.com/web/page.php?c=categoryname
I need to change it at:
www.mysite.com/web/page/categoryname.html
I am using this code but it doesn't work:
RewriteEngine on
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ page.php?c=$1 [L]
How can I fix the above code to correctly rewrite my URLs?
EDIT:
Thanks to Ulrich Palha
i manage to redirect the html files, but css,js etc are not redirect and the website looks rly bad, this is the code i am using now, till i will get the one who will redirect css too
RewriteEngine On
RewriteBase /web/
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
BUMP ANY HELP PLEASE?
Add the following to the .htaccess file in the web directory of your site, replacing the rules you have above.
RewriteEngine On
RewriteBase /web/
#do not process css/js etc
RewriteCond %{REQUEST_URI} \.(css|js|jpg|png) [NC]
#alternatively you could skip all existing files. Comment previous line and uncomment next to do so
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
Your regex in the rule is wrong — you are missing /page, adding an optional final slash that you shouldn't be, and forgetting to remove the .html part. Also your RewriteBase shouldn't have a final slash and your rule shouldn't have a leading slash.
It total, it should be this:
RewriteEngine on
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)\.html$ page.php?c=$1 [L]

Categories