I have a website, it contains many urls, like:
/public
/public/images/...
/questions/...
/answers/...
...
I want to add some cache headers to all the urls except `/public/.*'. But I don't know how to write this regex.
I tried:
^(?<!\Q/public/E)/.*
But it not works.
.htaccess under apache? try:
<LocationMatch "^(?!/public/)[^\.]+$">
Header set Cache-Control max-age=7200
</LocationMatch>
Related
I have urls like www.example.com/de/something and I need to redirect to www.example.com everything that starts with /de/.
At the moment I have done this
redirect 301 /de http://example.com
and it redirect all links but just removing /de part and result is www.example.com/something.
How to fix this?
Thanks
redirect directive matchs rest of the uri and appends it to the target, you can use RedirectMatch to redirect a specific uri
redirectMatch 301 ^/de/? http://example.com
If you want /de/ in the target you should have specified so, becasue Redirect will include in the target everything "after" what you have matched.
For a different virtualhost as the destination you want this:
Redirect 301 /de/ http://example.com/de/
or
Redirect 301 /de http://example.com/de
If what you want is redirect inside the same virtualhost /de to /, then use a negative lookahead.
RedirectMatch ^(?!de) http://example.com/
If the context is .htaccess, for virtualhost It would be:
RedirectMatch ^/(?!de) http://example.com/
Note: I use /de/ originally because that's what you describe in your question, and also I match slashes in the target. Both source and target without slashes would be fine too for cases like /desomething or /de/something. In any case, always match slashes or the lack of them.
Note2: Do not use .htaccess to redirect unless you are not the admin of the site. It just complicates things and adds unnecessary overhead since the file/s need to be checked a number of times per hit.
Background:
I recently changed in a DokuWiki installation every string which contains a specific product name to another. Now I have to redirect hard-coded links to the new URL.
The URLs look like:
past: example.com/wiki/doku.php?id=sales:producta:descr:producta
present: example.com/wiki/doku.php?id=sales:productb:descr:productb
Issue:
I should replace the string producta to productb. This string can occur several times in a URL. Therefore it should be replaced regardless the number of occurences.
I found several ways to replace a unique string. But I need to replace every single occurrance of this string in the URL. A working replacement before the question mark ist possible, but i didn't found a solution to manipulating the querystring like this.
Is there a way to achieve this replacement?
It's quite tricky, but it can be done. Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options -Multiviews
RewriteEngine On
# If the query string contains producta, capture it to %1
RewriteCond %{QUERY_STRING} ^(.+?producta.*)
# ... and bring it down to the url, temporarily inserting ##
RewriteRule ^(.+) $1##%1? [DPI]
# Rewrite product a to product b but only if ## is in the string
RewriteRule ^([^#]+##.*)producta(.*) $1productb$2 [N]
# Remove ##
RewriteRule ^([^#]+)##(.*) $1?$2 [L]
This assumes that mod_rewrite is both installed and activated for .htaccess files.
If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
I have the following .htaccess file on our xxx.yyy.edu site, in the root.
<FilesMatch "(.+)\.php">
AuthType shibboleth
ShibRequestSetting requireSession 1
ShibUseHeaders On
Require valid-user
ShibRequestSetting redirectToSSL 443
</FilesMatch>
This should only match files that end in .php, and match all files that end in .php, then force authentication. That part of it appears to work fine.
However, it also matches the root url, http://xxx.yyy.edu and http://xxx.yyy.edu/. Which isn’t a good thing, since it is forcing the root of the site to be authenticated.
It does not match xxx.yyy.edu/index.htm, or any other url that has anything after the domain name, like xxx.yyy.edu/students.htm or even xxx.yyy.edu/x
I’ve tested it in www.regexr.com, and it does not match the root url there.
Any thoughts/suggestions appreciated.
You are getting authentication dialog because on your landing page default page is set to index.php.
Have this FilesMatch with negative lookahead to avoid index.php from authentication:
<FilesMatch "^(?!index\.php$).+?\.php$">
AuthType shibboleth
ShibRequestSetting requireSession 1
ShibUseHeaders On
Require valid-user
ShibRequestSetting redirectToSSL 443
</FilesMatch>
Sounds like perhaps it is matching the newline with the "dot" (a la XRegExp). Try something like \w+:[\d\w\s\./]+\.php.
I have a url that google has indexed that looks like http://example.com/someth…/ with those ellipses just like that. No, it's not just 3 periods. I'm trying to 404 the page in the .htaccess but I don't know how to match an ellipses.
I have tried RedirectMatch 404 ^/someth\x2026/ but it doesn't work.
Anyone know how to match an ellipses using regex?
try this
<FilesMatch /include(/?|/.*)>
Order allow,deny
Deny from all
</FilesMatch>
RedirectMatch 404 ^/include(/?|/.*)$
Try using URL encoding (of the UTF-8 encoded char), eg:
^/?someth%E2%80%A6/
Check your server log for the exact URL the request is using.
My /train directory is aliased to a script in httpd.conf by:
WSGIScriptAlias /train /some-path/../django.wsgi
And it works well, except for one problem. If a user goes to /train (with no trailing slash) it will not redirect him to /train/, but will just give him the right page. This is a problem because this way the relative links on this page lead to the wrong place when no trailing slash was used to access it.
How can this be worked out?
Thanks.
I'm using something like this for redirecting /train to /train/, what I do is redirecting all the URL than doesn't end with / to /train/.
<Location "/train">
Order deny,allow
Allow from all
RewriteEngine on
RewriteRule !^.*/$ /train/ [R]
</Location>
WSGIScriptAlias /train /some-path/../django.wsgi
If you just need to redirect from /train to /train/ and not from every subdirectory without a trailing slash, then there's a simpler solution using the RedirectMatch directive:
RedirectMatch ^/train$ /train/
Set your urlconf to accept train/ as valid instead, then make train lead to a generic redirect to /train/.