rewrite url, remove segment - .htaccess

I'm trying to remove /uploads from a url. I have followed many examples but none of them work.
I think this one is the closest and should actually work but nothing is happening.
RewriteRule ^/uploads/(.+)$ /$1 [QSA]
here is what I have in the htaccess
# Enable URL rewriting for pretty URLs
php_flag magic_quotes_gpc off
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
RewriteEngine on
RewriteBase /
RewriteRule ^/uploads/(.+)$ /$1 [QSA]
RewriteRule !\.(php|js|ico|gif|jpg|png|css|html|cgi|pdf|doc|xls|docx|xlsx|ppt|tff|svg|eot|woff|swf|mp3|wma|wav|MP4|AIFF|AAC|m4a|m4p|AVI|MOV|MPG|MPEG)$ index.php [NC]
#RedirectMatch ^/$ /misc/offline.php [NC]
I should go from:
http://example.com/storage/uploads/droom.pdf
To:
http://example.com/storage/droom.pdf

Try replacing this line:
RewriteRule ^/uploads/(.+)$ /$1 [QSA]
with this:
RewriteRule ^(.*?)/?uploads(.*)$ /$1$2 [L]

Related

How do I correctly handle multiple capturing groups in .htaccess?

I'm trying to get:
example.com/curriculum/<curriculum name>/unit/<unit name>/
to rewrite to:
example.com/curriculum/unit/?c=<curriculum name>&u=<unit name>
so far I have:
RewriteRule ^/curriculum/([A-Za-z0-9_-]+)/unit/([A-Za-z0-9_-]+)/?$ https://www.example.com/curriculum/unit/?c=$1&u=$2
Considering that:
RewriteRule ^curriculum/([A-Za-z0-9_-]+)/?$ https://www.example.com/curriculum/?c=$1
works as expected, I assume my syntax is off, as when loading, I just get error 404, with the URL not rewritten from its original format. To clarify, /curriculum/unit does exist.
Any guidance or advice on what I need to do to make this work? Thanks!
Update
Full .htaccess file as requested:
### activate mod_expires
ExpiresActive On
### Expire .gif's 1 month from when they're accessed
ExpiresByType image/gif A2592000
### Expire everything else 1 day from when it's last modified
### (this uses the Alternative syntax)
ExpiresDefault "modification plus 1 day"
### Apply a Cache-Control header to index.html and styles.css
###Cross-Site Scripting protection
Header always set X-Xss-Protection "1; mode=block"
###Disallow websites to frame legacycars - user protection
Header always append X-Frame-Options SAMEORIGIN
<IfModule mod_headers.c>
#No Cache browser revalidates on each request and fetches new version IF contents change based on ETag or Last-Modified response
<FilesMatch "\.(html|php)$">
Header set Cache-Control "no-cache"
</FilesMatch>
#DAY Stock and Cover images expire at the end of each day, but must be checked for changes
<FilesMatch "\.(jpg|jpeg)$">
Header set Cache-Control "max-age=86400, public, must-revalidate"
</FilesMatch>
#WEEK Icons and badges expire after a week
<FilesMatch "\.(ico|png|gif|swf|svg|xml)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
#MONTH Site framework expires after a month, but must be checked for changes
<FilesMatch "\.(js|css|json)$">
Header set Cache-Control "max-age=2628000, public, must-revalidate"
</FilesMatch>
#Hide .credentials
<FilesMatch ".credentials.php">
Order allow,deny
Deny from all
</FilesMatch>
Options +FollowSymLinks -MultiViews
ErrorDocument 404 https://www.example.com/error/404.html
ErrorDocument 403 https:/www.example.com/error/403.html
# Turn mod_rewrite on
RewriteEngine On
#Rewrite curriculum and unit to GET vars
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^curriculum/([\w-]+)/unit/([\w-]+)/?$ curriculum/unit/?c=$1&u=$2 [L,QSA,NC]
RewriteRule ^curriculum/([\w-]+)/?$ curriculum/?c=$1 [L,QSA,NC]
# Externally redirect direct client requests for subdomain-subdirectory URLs
# to subdomain URLs without subdomain-subdirectory URL-path
Redirect permanent /sd_manage/ https://manage.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^manage(.*)$ https://manage.example.com [R=301,L]
RewriteRule ^manage/(.*)$ https://manage.example.com/$1 [R=301,L]
# Rewrite root subdomain to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
# Rewrite valid subdomains to the secure site
RewriteCond %{HTTPS} off
# RewriteCond %{HTTP_HOST} ^(www|manage)\.example\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php71” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php71 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
You may keep these 2 rewrite rules:
ErrorDocument 404 /error/404.html
ErrorDocument 403 /error/403.html
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Externally redirect direct client requests for subdomain-subdirectory URLs
# to subdomain URLs without subdomain-subdirectory URL-path
RewriteRule ^sd_manage/ https://manage.example.com [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^manage(/.*)?$ https://manage.example.com$1 [R=301,L,NC,NE]
# Rewrite root subdomain to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# Rewrite valid subdomains to the secure site
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
#Rewrite curriculum and unit to GET vars
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^curriculum/([\w-]+)/?$ curriculum/?c=$1 [L,QSA,NC]
RewriteRule ^curriculum/([\w-]+)/unit/([\w-]+)/?$ curriculum/unit/?c=$1&u=$2 [L,QSA,NC]

.htaccess pretty url with and without parameter and slash

I have below code in my .htaccess file. When I open https://example.com/tran it shows 404 page but adding slash in url like https://example.com/tran/ works.
How can I make https://example.com/tran to redirect to https://example.com/tran/ and https://example.com/tran/parameter1 both work because also want to get parameter from url using $_GET method when it is available in url.
Options All -Indexes
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
<FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>
RewriteRule tran/(.*) transaction.php?id=$1
RewriteRule tran/(.*)/ transaction.php?id=$1
RewriteRule addr/(.*) address.php?id=$1
RewriteRule addr/(.*)/ address.php?id=$1
You can use:
RewriteRule tran(?:/(.*?)/?)?$ transaction.php?id=$1
RewriteRule addr(?:/(.*?)/?)?$ address.php?id=$1
instead of your 4 rules

Need to to run web application on local environment

I don't have information about how .htaccess is works. I have below script in my .htaccess. This web I downloaded from live now I need to run it on local environment (localhost). I have windows and xampp I already configured db for this web application but because of .htaccess the web application is not working when I execute application it redirects to "http://www.localhost/xampp/" please help to resolve the issue I will be very thank full.
Application address: http://www.localhost/abc
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
##RewriteRule ^index.html$ home [L,NC]
RewriteRule ^home$ index.php?pageid=91
RewriteRule ^home$ http://www.example.com/Home
Redirect /index.html http://www.example.com/home
RewriteRule ^home$ /index.php?pageid=91
RewriteRule ^products$ /product-details.php?pageid=92&ID=2
RewriteRule ^services$ /subpage.php?pageid=94
RewriteRule ^SOLUTIONS$ /subpage.php?pageid=105
RewriteRule ^ABOUT-US$ /subpage.php?pageid=113
RewriteRule ^contact$ /contact.php?pageid=115
RewriteRule ^contact$ /contact.php?pageid=115&webtolead=1
RewriteRule ^customer-relationship-management$ /solutions.php?pageid=116
RewriteRule ^enterprise-mobility-management$ /solutions.php?pageid=117
RewriteRule ^application-performance-management$ /solutions.php?pageid=118
RewriteRule ^quantum-mes$ /solutions.php?pageid=129
RewriteRule ^consulting$ /solutions.php?pageid=119
RewriteRule ^implementation$ /solutions.php?pageid=120
RewriteRule ^support$ /solutions.php?pageid=121
RewriteRule ^Company-Overview$ /subpage.php?pageid=122
RewriteRule ^President-Message$ /subpage.php?pageid=123
RewriteRule ^Clients$ /clients.php?pageid=124
RewriteRule ^News$ /news.php?pageid=125&page=0
RewriteRule ^Careers$ /careers.php?pageid=126
RewriteRule ^sitemap$ /sitemap.php?pageid=128
RewriteRule ^--Pivotal-CRM$ /product-details.php?pageid=92&ID=2
RewriteRule ^Sugar-CRM$ /product-details.php?pageid=92&ID=3
RewriteRule ^Airwatch-EMM$ /product-details.php?pageid=92&ID=5
RewriteRule ^Compuware-APM$ /product-details.php?pageid=92&ID=6
RewriteRule ^IStorage$ /product-details.php?pageid=92&ID=7
RewriteRule ^Astute-Solutions$ /product-details.php?pageid=92&ID=9
RewriteRule ^MS-Dynamics$ http://www.example.com/products
RewriteRule ^Test-Product$ /product-details.php?pageid=92&ID=10
RewriteRule ^News-([0-9]+)$ /news-details.php?pageid=125&id=$1
RewriteRule ^NEWS-([0-9]+)$ /news.php?pageid=125&page=$1
RewriteRule ^Careers-([0-9]+)$ /job-details.php?pageid=126&id=$1
RewriteRule ^CAREERS-([0-9]+)$ /job-details.php?pageid=126&page=$1
<IfModule mod_expires.c>
ExpiresActive On
############################################
## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 1 week"
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
Header unset ETag
Header unset Last-Modified
</IfModule>
FileETag None
<FilesMatch "\.(ico|gz|JPG|jpg|jpeg|png|gif|js|css|swf)$">
Header unset Cache-control
Header set Expires "access plus 1 week"
</FilesMatch>
The redirect happens in line 2 and 3. If you remove them it should go away.
This is not a programming question. Also, the documentation for mod_rewrite is readily available.
Change your base to match the folder you're serving from:
RewriteBase /abc/
Comment out all the unused rules, like the extra ^home$ ones. Add [L] flags to all the rewriterules so you stop processing on the first match.

How to redirect requests to files with .xml extension

On my site I want to block access to my xml files
For example, when the following files are requested
http://example.com/a_File.xml
or
http://example.com/some_file.xml
I want browser to redirect to a different page http://example.com/different_Page.html.
Here is what I have in .htaccess so far:
SetEnvIf Request_uri "\.xml$" blocked
RewriteEngine ON
RewriteCond %{ENV:blocked} ^blocked
RewriteRule (.*) 404.php [R=301]
It does not stop it, I still can view my xml file.
RewriteEngine on
RewriteRule \.xml$ /different_Page.html [NC,R=301,L]
Use SetEnvIfNoCase Directive to match against Request_URI String in case-insenstive manner :
SetEnvIfNoCase Request_uri "\.xml$" blocked
RewriteEngine ON
RewriteCond %{ENV:blocked} 1
RewriteRule ^(.*)$ 404.php [NC,L,R=301]
RewriteEngine ON
RewriteCond %{REQUEST_URI} \.xml$
RewriteRule (.*) 404.php [R=301]

mod_rewrite keep rewriting recursilvely

I just want to append the url with home but it is happening in recursion and i get the browser saying too many redirects
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^test main.html
RewriteRule rest/(.*)$ /home/rest/$1 [R=301]
</IfModule>
so i want
localhost/rest/abc.php
to
localhost/home/rest/abc.php
Remove the slash before home.
RewriteRule ^rest/(.*)$ home/rest/$1 [R=301]

Resources