.htaccess RewriteRule from download.php?token=something into /download/something - regex

I have this URL:
http://www.example.com/subf/subf2/download.php?token=6309838552568
It is possible to rewrite this url into this url?
http://www.example.com/subf/subf2/download/6309838552568
Note: If someone types a URL like this:
http://www.example.com/subf/subf2/download/6309838552568/sbuf3/subf4
i want to show them an error page.
Answer below
Hint: Include files like JS and CSS need to be refer in absolute paths.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT/subf/subf2/ directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subf/subf2/
RewriteRule ^(download)/(\w+)/?$ $1.php?token=$2 [L,QSA,NC]
# to take care of css, js, images
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^download/(.+)$ /subf/subf2/$1 [L,R=301,NC]

Related

Use .htaccess to redirect to directory URL but show the URL as if in root

I currently have a CRUD that has the following file structure:
The index.php file redirects to the add-product.php, delete.php and other files. The problem is, the URL looks like: myapp.com/resources/views/add-product but I want it to look like myapp.com/add-product. I found other StackOverflow posts similar to this, but it redirects to the desired link, instead of redirecting to the correct link but showing the desired one, and because of that the site doesn't work ("The requested URL was not found on this server"). The .htaccess file looks like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^resources/(.*)$ /$1 [L,NC,R]
I'm also using the file to be able to remove the .php from the URLs. The last line is the one handling the wrong redirection. How can I go about this? Thank you for your time.
You may use it like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/
# external redirect to remove /resources/views/ from URLs
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=302]
# internal rewrite to add /resources/views/ and .php to show content
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Redirect pages in a subfolder to a new page in same subfolder htaccess

I need to redirect several pages to a new page, the easiest way to explain is this...
http://www.example.com/numbers/0800/bronze.html TO http://www.example.com/numbers/0800/bronze-new.html
http://www.example.com/numbers/0845/bronze.html TO http://www.example.com/numbers/0800/bronze-new.html
Any ideas how to do this in one htaccess rule? Thanks!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !-new\.html$ [NC]
RewriteRule ^(numbers/.+?)\.html$ /$1-new.html [L,R,NC]

Htaccess redirect from http to https and get params

I need to redirect this:
http://www.mysite.com/pages/addtocart.php?id=XXXX
to this
https://www.mysite.com/checkout/cart/addsku?id=XXXX
Up to now, this is what I have:
RewriteRule ^/pages/addtocart.php?(.*)$ https://www.mysite.com/checkout/cart/addsku?$1 [R=301,L]
But it's not working.
Help?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^pages/addtocart\.php$ https://%{HTTP_HOST}/checkout/cart/addsku [L,R=302,NC]
PS: Query Parameter will automatically be carried over to new URL.

I need to hide directory from url using htaccess

I need to remove directories from my local website url:
Current url : http://localhost/example/trunk/frontend/www
Needed url : http://localhost/example/frontend
I need to remove trunk and www folders from url without changing files (css, js, images, ...etc) paths.
Can anyone help me please?
Thanks
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/example/trunk/[^/]*/www [NC]
RewriteRule ^(example)/([^/]+)(/.*|)$ /$1/trunk/$2/www$3 [L,NC]
UPDATE:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /example/
RewriteRule ^((?!trunk)[^/]+)(/.*|)$ trunk/$2/www$3 [L,NC]

mod rewrite for pretty urls and also hide index.php

How can i mod-rewrite to obtaining a pretty url like below
example.com
contains a form to get a text value and pass it another file for processing that file is in directory as web but i need to change that as pretty urls
example.com/web/index.php?url=mydomain.com
to
example.com/web/mydomain.com
example.com/web/newdomain.com
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+web/index\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /web/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^web/(.+?)/?$ /web/index.php?url=$1 [L,QSA]