How to rewrite theme path with difference in folder using htaccess - .htaccess

I'm having trouble with .htaccess config...
List files and folders content:
/
src/
css/
admin/styles.css
user/styles.css
js/
admin/init.js
user/init.js
I want to access the file/folder path will matching paths rewrite
http://domain/src/css/staff/styles.css => src/css/admin/styles.css
http://domain/src/css/styles.css => src/css/user/styles.css
http://domain/src/js/staff/styles.js => src/js/admin/init.js
http://domain/src/js/styles.js => src/js/user/init.js
And here is my code:
RewriteRule ^src/staff/(.*)$ src/admin/$1 [L]
RewriteRule ^src/(.*)$ src/user/$1 [L]
But apparently things not working, there was an error has occurred "500 Internal Server Error".
If you just take a look at my problem and share a bit of your science, I'd be very grateful. Thanks!

With more regex matching you can use these 2 rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(src)/(js|css)/staff/([^/.])+\.(css|js)$ $1/$2/admin/$3.$4 [L,NC]
RewriteRule ^(src)/(js|css)/([^/.])+\.(css|js)$ $1/$2/user/$3.$4 [L,NC]

Related

URL rewrite in subfolder css not working

How are you today,
I have a problem in url rewriting..
I Upload my website on pesco sub-directory, Like www.example.com/pesco and my css path like this /css/style.css but when site run css must be like www.example.com/pesco/css/style.css but my css showing like this www.example.com/css/style.css url rewrite missed the pesco sub-directory
Please any one give me the solution
My Code is below you can see.
ErrorDocument 404 /notfound.php
RewriteEngine on
RewriteBase /pesco/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html index.php
RewriteRule ^aboutus.html aboutus.php
RewriteRule ^([a-zA-Z0-9_-]+).html$ products.php?catid=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ detail.php?category=$1&product=$2
I think your error is just a reference problem. You can try ./css/style.css as this means it looks to find css as a directory inside the same directory as the file requesting it, whereas /css/style.css is essentially looking in the root directory

How to always redirect to subfolder using htaccess

I want to redirect visitors who are viewing index (not other files) to a sub folder in every conidition. At the moment I'm using the code below in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteRule ^$ /dir [L,R=301]
</IfModule>
But it only works on the main domain but doesn't work when we have the script itself in a subfolder. How should I modify this code to works everywhere?
The previous code works like below when installed on the root folder:
http://www.domain.com => http://www.domain.com/dir
Which is fine, but when we have it on sub, it works like below:
http://www.domain.com/sub => http://www.domain.com/dir
That means it move one level up and then locate for dir and that's the failure.
Use that:
# Change in subfolder:
RewriteBase /sub/
RewriteRule ^$ dir [L,R=301]
In root .htaccess for each subfolder:
RewriteRule ^([^/]+)/$ $1/dir/ [L,R=301]

Redirecting url using .htaccess in a subdirectory

I have a sub directory named "blog". I want to clean the url when accessing this directory in web browser. So I want that when I type
www.example.com/blog/index.php?id=1
The webbroswer must show like this:
www.example.com/blog/posts/1
I successfully implemented this when there was no sub directory, but in sub directory case it shows me a 404 error.
Here what I have done in my .htaccess file to make it work when I was implementing it in the root directory(not in the subdirectory).
RewriteEngine On
RewriteRule ^posts/([^/]*)$ /index.php?id=$1 [L]
And I wrote my url in the index.php file as
echo '' . $row['title'] . '';
But the problem is when the same thing I tried to implement in a sub directory named "blog", it shows me a 404 error.
I tried this rule in .htaccess., but it did not work.
RewriteCond %{REQUEST_URI} !^/blog/.*$
RewriteRule ^(.*)$ /blog/$1 [L]
I am keeping my .htaccess file in the sub directory named blog.
I tried many questions in StackOverflow, but none seems to solve my problem.
Any help would be appreciated.
Add a rewwrite base to the rules that you had:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^posts/([^/]*)$ index.php?id=$1 [L]
These would be in the htaccess file in the directory /blog/
And make sure in your index.php, you are generating links like:
echo '' . $row['title'] . '';

htaccess 404 issue

I am working with a CMS, and up until today its been fine.
But i have discovered that the mod rewirte only works if the website is in the root directory. If I put the entire CMS into a folder, i get a 404.
Please help!
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^([^/\.]+)/?$ /index.php?1=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5&6=$6 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5&6=$6&7=$7 [L]
Then you need to add the subfolder to your rewrite rule like this, for all of your rules.
RewriteRule ^([^/\.]+)/?$ /subfolder/index.php?1=$1 [L]
With your configuration apache will search for the index.php file in the root directory
Your script is extracting data that are between slashes like:
/test1/test2/ transfers into /index.php?1=test1&2=test2
but since you started using folder it works like:
/folder/test1/test2/ transfers into /index.php?1=folder&2=test1&3=test2
so the folder name is breaking your structure, you will need to fix every line with folder name or expression like this:
RewriteRule ^[^/\.]+/([^/\.]+)/?$ /folder/index.php?1=$1 [L]
So you will need to fix both regular expression and new path on every line, just replace folder with name of your folder and add [^/\.]+/ at start
If that doesnt work you may just need to fix regular expression only without adding /folder to the second part of line

mod_rewrite and GoDaddy

I really have only a fair idea of what I am doing. Sorry.
This is what I did to rewrite URLs like these:
http://example.com/foo/bar/news/1-category-name/2-item-name
http://example.com/foo/bar/news/1-category-name
into these:
http://example.com/foo/bar/news.php?newsID=2
http://example.com/foo/bar/news-categories.php?categID=1
respectively
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /foo/bar
RewriteRule ^news\/([^\/]+)/([0-9]+)-([^\/]+)$ news.php?newsID=$3
RewriteRule ^news\/([0-9]+)-([^\/]+)$ news-categories.php?categID=$1
</IfModule>
But whenever I try this URL:
http://example.com/foo/bar/news/16-news-category-1/11-title
It always returns a 404 error:
The requested URL /foo/bar/news.html/16-news-category-1/11-title was not found on this server
Why is it trying to find news.html? What am I doing wrong?
I don't know if this is relevant but example.com is virtual (I really don't know the term). The actual files can also be accessed via http://real-domain.com/qux/. So you'd do http://real-domain.com/qux/foo/bar/news/1-category-name/2-item-name to get to 2-item-name.
Does it also help to say that this script actually runs on my development machine?
Development:
PHP v5.3.5
Apache v2.2
Live:
PHP v5.3.6
Apache v2.2
You this code in your .htaccess:
RewriteEngine on
RewriteRule ^news/([0-9]+)-([a-z\-]+)-([0-9]+)/([0-9]+)-([a-z\-]+)(\/?)$ /news.php?newsID=$4 [L,NC]
RewriteRule ^news/([0-9]+)-([a-z\-]+)/([0-9]+)-([a-z\-]+)(\/?)$ /news.php?newsID=$3 [L,NC]
RewriteRule ^news/([0-9]+)-([a-z\-]+)-([0-9]+)(\/?)$ /news-categories.php?categID=$1 [L,NC]
RewriteRule ^news/([0-9]+)-([a-z\-]+)(\/?)$ /news-categories.php?categID=$1 [L,NC]
so when someone goes to:
news/11-category-12 or
news/11-category-12/ or
news/11-category or
news/11-category/
it goes to: news-categories.php?categID=11
and when someone goes to:
news/11-category-12/1-title or
news/11-category-12/1-title/ or
news/11-category/1-title or
news/11-category/1-title/
it goes to: news.php?newsID=1
Also, make sure the .htaccess is at the root of the public_html folder as well of news.php and news-categories.php

Resources